Awk: System command not working in awk

Hi,
I have around 10 files in a folder in which I want to change the file format from tab(\t) to pipe(|) with some changes in the fields as well. Below is the code, while tmp file is getting generated but move command is not working, please help

Following is the code

 awk -F"\t" '{print $1,$2,substr($3,1,2)substr($3,4,2)substr($3,7,2),substr($4,7,4)substr($4,1,2)substr($4,4,2),$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31 >> FILENAME".tmp" }END{system("mv " FILENAME".tmp" " " FILENAME)} ' OFS="|" *.PRC
 

Sample Input file

 65535   1558    14:46:55        11/19/2014      8926877659      30239933826126  8926877659      45      0       0       0       0       20272   00000   2   802DBD5B 405891369800923 postpaid        null    null    20272   20272   OUT     14:47:40        561     561     144     null    405899153999998 405899153999998      63581439
65535   65535   14:46:21        11/19/2014      9143110710      8926328080      9143110710      88      0       0       0       0       23702   15930   2   8076C355 405891373813936 postpaid        null    919153881217    23702   23702   OUT     14:47:49        295     295     159     null    405899153999998 405899153999998      58123387
65535   302     14:46:30        11/19/2014      8642035475      999     8642035475      84      HTLN    0       0       0       21170   00000   2       80CD9C6C     405891961388656 postpaid        null    null    21170   21170   OUT     14:47:54        562     562     144     null    405899153999998 405899153999998      47000903
65535   311     14:47:50        11/19/2014      9143110721      31809143110663  9143110721      28      0       0       0       0       23702   00000   2   8063898E 405891373813947 postpaid        null    null    23702   23702   OUT     14:48:18        295     295     144     null    405899153999998 405899153999998      63842634
65535   302     14:48:05        11/19/2014      9143110266      30618906570722  9143110266      21      0       0       0       0       22912   00000   1   80D295D1 405891350843985 postpaid        null    null    22912   22912   OUT     14:48:26        561     561     144     null    405899153999998 405899153999998      45469437

 

It's perilous to alter your originals. One mistake and you've trashed all your input data. I would begin by renaming all your files to FILENAME".tmp" and create new ones with the original filenames instead; delete your originals once you've tested the new data and have made sure it's worked to your satisfaction.

END does not work that way, it runs once and only once, after the very last file is read. You need to watch for the filename changing instead. Or, better yet, move that function outside of awk.

It's safe and normal to rename a file while you have it open, on UNIX. I've tested this and it will end up with the originals in *.tmp and the new files in *.prc. It will warn you about running it twice in a row without checking the data.

for FILE in *.tmp
do
        [ -f "$FILE" ] || break
        echo ".tmp files remaining from last run -- check and delete if originals OK" >&2
        exit 1
done

awk -v OFS="|" '(!F) || (F != FILENAME) { if(F) close(F) ; F=FILENAME ; system("mv "F" "F".tmp"); }  { print ... > F }' *.ext

I think I found the "bug".
Your awk command incl. system() call will work fine if you apply it to only one file at a time.

See what happens if you have more files:

$ touch file{1..9}
$ ls file{1..9}
file1  file2  file3  file4  file5  file6  file7  file8  file9
$ awk '{next;}END{print FILENAME}' file{1..9}
file9
$

Do you recognize the dilemma?

Possible solution:

for file in *.PRC; do
 awk ... $file
done

Hope this helps.

---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ----------

Doh! :eek: Too late by two hours :rolleyes: