Skip to main content
Add automation for-loop
Source Link
grebneke
  • 4.8k
  • 27
  • 20

If I understand correctly, you have a file called wrongdatefile20140228.csv and you need to create a new file called correct_date20140228.csv? Assuming bash shell:

$ ls *.csv
wrongdatefile20140228.csv

Assign filename to variable $fn and use Parameter Expansion

$ fn=wrongdatefile20140228.csv
$ awk '...' "$fn" > "${fn/#wrongdatefile/correct_date}"

Result:

$ ls *.csv
correct_date20140228.csv    wrongdatefile20140228.csv

To automate this for all files wrongdatefile*.csv in current directory, skipping previously processed files:

for fn in wrongdatefile*.csv; do 
    newfn="${fn/#wrongdatefile/correct_date}"
    [ -e "$newfn" ] || awk '...' "$fn" > "$newfn"
done

If I understand correctly, you have a file called wrongdatefile20140228.csv and you need to create a new file called correct_date20140228.csv? Assuming bash shell:

$ ls *.csv
wrongdatefile20140228.csv

Assign filename to variable $fn and use Parameter Expansion

$ fn=wrongdatefile20140228.csv
$ awk '...' "$fn" > "${fn/#wrongdatefile/correct_date}"

Result:

$ ls *.csv
correct_date20140228.csv    wrongdatefile20140228.csv

If I understand correctly, you have a file called wrongdatefile20140228.csv and you need to create a new file called correct_date20140228.csv? Assuming bash shell:

$ ls *.csv
wrongdatefile20140228.csv

Assign filename to variable $fn and use Parameter Expansion

$ fn=wrongdatefile20140228.csv
$ awk '...' "$fn" > "${fn/#wrongdatefile/correct_date}"

Result:

$ ls *.csv
correct_date20140228.csv    wrongdatefile20140228.csv

To automate this for all files wrongdatefile*.csv in current directory, skipping previously processed files:

for fn in wrongdatefile*.csv; do 
    newfn="${fn/#wrongdatefile/correct_date}"
    [ -e "$newfn" ] || awk '...' "$fn" > "$newfn"
done
Source Link
grebneke
  • 4.8k
  • 27
  • 20

If I understand correctly, you have a file called wrongdatefile20140228.csv and you need to create a new file called correct_date20140228.csv? Assuming bash shell:

$ ls *.csv
wrongdatefile20140228.csv

Assign filename to variable $fn and use Parameter Expansion

$ fn=wrongdatefile20140228.csv
$ awk '...' "$fn" > "${fn/#wrongdatefile/correct_date}"

Result:

$ ls *.csv
correct_date20140228.csv    wrongdatefile20140228.csv