Use portion of file name as new file name


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Use portion of file name as new file name
# 1  
Old 12-18-2016
Use portion of file name as new file name

Hello, This is my first posting and attempt to create a loop using Unix and awk commands. So far this is what I have:
Code:
 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:
Code:
  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

Last edited by RudiC; 12-19-2016 at 06:45 AM.. Reason: Corrected /code tag.
# 2  
Old 12-19-2016
Why not make use of awk's predefined FILENAME variable and adapt your script like
Code:
awk '... >...FILENAME...' *.txt

# 3  
Old 12-19-2016
I appreciate your answer, however,I have tried multiple iterations of the following changes to no avail.
Code:
"#!/usr/bin/awk -f"
For EACHFILE in * 
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_" File "_ACTUAL_"months[substr($3,0,3)]"20" 
substr($3,5,2)"_RR.txt"}> File'*.txt
Done

Would you mind being very specific, as I mentioned I am new to UNIX and don't know where to place the changes. I keep getting the following error:
Syntax Error Line 1.
Smilie
# 4  
Old 12-19-2016
My first though would be the quotes in the first line "#!/usr/bin/awk -f"

Are they really there or is it some problem with pasting? Try taking them out to leave the first line being just #!/usr/bin/awk -f



Robin
# 5  
Old 12-19-2016
I have remove the quotes. I get this:
Code:
-bash: Done: Command not found


Last edited by rbatte1; 12-19-2016 at 10:24 AM.. Reason: Added CODE tags for output/error
# 6  
Old 12-19-2016
The shell commands are do and done, but you have them as Do and Done

I suspect that the editor you are using automatically capitalises the first character of what it sees as a sentence. This is usually a bad thing when writing code, especially as most of these helpful writing editors can shove lots of control characters into what you want as code.

I would suggest using a plain text editor, such as vi or ed - or at worst notepad on Windows.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 7  
Old 12-19-2016
Quote:
Originally Posted by Raul_Rodriguez
I appreciate your answer, however,I have tried multiple iterations of the following changes to no avail.
Code:
"#!/usr/bin/awk -f"

erroneous and not needed. If you want to use awk in the "shebang", the script needs to be pure awk.

Quote:
Code:
For EACHFILE in * 
Do

not used in script and thus obviously not needed

Quote:
Code:
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_" File "_ACTUAL_"months[substr($3,0,3)]"20" 
substr($3,5,2)"_RR.txt"}> File'*.txt

WHAT is File? And, WHY two redirections?


Clean and consistent indenting sometimes helps track down errors. Try running this from the command line (i.e. DON'T put it into a script file). It should show you the target files for the redirections without actually creating and populating those files:
Code:
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 "_ACTUAL_"months[substr($3,0,3)]"20" substr($3,5,2)"_RR.txt"
        }
' *.txt


Last edited by RudiC; 12-19-2016 at 10:42 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

2. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

3. UNIX for Advanced & Expert Users

Removing portion of file name

Hi , I am getting file name like ABC_DATA_CUSTIOMERS_20120617.dat ABC_DATA_PRODUCTS_20120617.dat Need to convert CUSTIOMERS.dat PRODUCTS.dat Help me how to do this. (7 Replies)
Discussion started by: reach_malu
7 Replies

4. Shell Programming and Scripting

Rename portion of file based on another file

Hello, I've been searching and reading, but I can't figure out how to solve this problem with my newbie skills. In my directory, I have a list of files (see dirlist.txt attachment) that I need to merge and rename. I have part of the code of the code figured out (see below). However, I... (3 Replies)
Discussion started by: anjulka
3 Replies

5. UNIX for Dummies Questions & Answers

Portion of a file in a new files

Hi, I need to devide one file into 3 files based on column numbers and put a string (FILE1, FILE2, FILE3) in the last..... Input file: Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10 Output1: Column1,Column3,Column6,Column4,Column5,FILE1 Output2:... (6 Replies)
Discussion started by: yale_work
6 Replies

6. Shell Programming and Scripting

Grep certain portion from the file

Dear Friends, Here I am with another difficulty. I have a flat file from which I wanna grep following pattern. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Statement Date : Blah blah Blah blah Blah blah Blah blah... (1 Reply)
Discussion started by: anushree.a
1 Replies

7. UNIX for Dummies Questions & Answers

Print a portion of file

Hi, I have a little problem. I am having a file with pattern like : asdf;ffgg;dfjfj;djdfjf;nnjj;djd;ssj; I just want to print the portion from last ";" upto the immediate previous ";". There are several ";" in my line. Please help me out... Thnx in advance (8 Replies)
Discussion started by: vanand420
8 Replies

8. Programming

Delete Portion of a file

hi i would like to know whether i can delete a part of a file in C for eg. if my file contained 1234567890 and i want to delete 456 so that it becomes 1237890 is there a way i can do this. well, one way i can achieve this is by creating a new file, copy whatever i want, then delete the... (2 Replies)
Discussion started by: sameersbn
2 Replies

9. Shell Programming and Scripting

Separate a portion of text file into another file

Hi, I have my input as follows : I have given two entries- From system Mon Aug 1 23:52:47 2005 Source !100000006!: Impact !100000005!: High Status ! 7!: New Last Name+!100000001!: First Name+ !100000003!: ... (4 Replies)
Discussion started by: srikanth_ksv
4 Replies

10. Shell Programming and Scripting

remove portion of file

Can anyone tell me how to remove a portion of a large file to smaller ones? What I have is a large file that was created becasue several similar files were joined together. Each individual file starts with MSG_HEAD. I want to take everything from MSG_HEAD up to were it says MSG_HEAD again and... (13 Replies)
Discussion started by: methos
13 Replies
Login or Register to Ask a Question