Hello, This is my first posting and attempt to create a loop using Unix and awk commands. So far this is what I have:
awk -F, 'BEGIN {months ["JAN"]="AP01"; months ["FEB"]="AP02"; months ["MAR"]="AP03";
months ["APR"]="AP04"; months ["MAY"]="AP05"; months ["JUN"]="AP06";
months ["JUL"]="AP07"; months ["AUG"]="AP08"; months ["SEP"]="AP09";
months ["OCT"]="AP10"; months ["NOV"]="AP11"; months ["DEC"]="AP12";}
{print > "a_STHCSPHFM_ACTUAL_"months[substr($3,0,3)]"20"
substr($3,5,2)"_RR.txt"}' STAVOSHFM.txt
It basically takes the 3 Characters representing the months and replaces them for AP01, etc. creating new files for each month. This works fine if I have only one file as a source.
However, I have multiple files, for example STCOCICANHFM.txt and STHCSPHFM.txt
What I want to do is replace STAVOSHFM.txt with the name of the first file being processed and then process the STCOCICANHFM and lastly STHCSPHFM replacing the source file name each time. The ideas is being able to identify the source file based on the newly created file.
So I tried this:
a "#!/usr/bin/awk -f"
for EACHFILE in $FILEPATH/${FILENAME}*.txt
Do
awk -F, 'BEGIN {months ["JAN"]="AP01"; months ["FEB"]="AP02"; months ["MAR"]="AP03";
months ["APR"]="AP04"; months ["MAY"]="AP05"; months ["JUN"]="AP06";
months ["JUL"]="AP07"; months ["AUG"]="AP08"; months ["SEP"]="AP09";
months ["OCT"]="AP10"; months ["NOV"]="AP11"; months ["DEC"]="AP12";} {print > "a_"FILENAME"months[substr($3,0,3)]"20"
substr($3,5,2)"_RR.txt"}' FILENAME
Done
However this does not work. Please tell me what do I need to correct this code. Thanks in advance