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
# 8  
Old 12-19-2016
The expected result should be 8 files:

Code:
a_STAVOSHFM_ACTUAL_AP06-2015_RR.txt

NOT

Code:
a_STHCPSHFM.txt_ACTUAL_AP022016_RR.txt

Again, the idea is to be able to identify the file but the file must comply with a very specific mask.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-19-2016 at 04:12 PM.. Reason: Raul_Rodriguez: Jumped to conclusion; RudiC: Added CODE tags.
# 9  
Old 12-19-2016
You haven't given us any context for when you want it to execute. What are your plans/needs for the code? Are you hoping to do any of these?:-
  • Inserting it into an existing application
  • Schedule it at a regular time
  • Call it on user request
  • Have an icon to click
  • Something else?


Robin
# 10  
Old 12-19-2016
I am currently using Cygwin64 Terminal for testing purposes, once successfully tested I will use this code or a variation of it in a software named Automic.
This software is object based and it can execute UNIX commands, but since the infrastructure were these commands will ultimately be executed is very complex, I do testing on an isolated environment with dummy files.

I was able to execute the code and see the results in Cygwin64 Terminal as I mentioned at the beginning of this thread; the issue I had was that the file name was hard coded in the awk script and I need to pick up the source file name (which is being split by the code into multiple files) while reusing a portion of its name in the newly created files.

Basically I want to make sure it runs without issues and review the newly created files for accurate formatting.
Again thank you for your help
# 11  
Old 12-20-2016
Wouldn't it be helpful if you listed the issues and posted the "accurate formatting" so we have something to work upon?

For your FILENAME extension problem in post#8 try to remove the extension like e.g.
Code:
        {TMP = FILENAME
         sub (/\.*$/, "", TMP)
         print "a_" TMP "_ACTUAL_"months[substr($3,0,3)]"20" substr($3,5,2)"_RR.txt"
        }

This User Gave Thanks to RudiC For This Post:
# 12  
Old 12-20-2016
Here is the whole process:
  1. Multiples files are deposited in an inbox folder
  2. File names vary but they are all .txt
  3. Each file has month and year identifier on each line
  4. The awk script splits the original file into multiple files
  5. The original file name must be inserted between the string I have defined in the awk statement(prefix,original filename, month dash year, suffix)
  6. This process has to be executed for each file deposited in the inbox
  7. The source data for each file is now easily identified by its original file name
The script I posted at the start of this thread successfully split a file, but the source file and the destination files were not dynamically named and it processed only one file.
The modified script below produces the split files but still has issues in the naming convention:
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";} 
TMP = FILENAME sub (/\.*$/, "", TMP) 
{print > "a_" TMP "_ACTUAL_"months[substr($3,0,3)]"20" substr($3,5,2)"_RR.txt"}' *.txt

The original files are
Code:
  • STAVOSHFM.txt
  • STCHSPHFM.txt

The results are as follows:
Code:
  • a_STAVOSHFM.txt1_ACTUAL_AP022016_RR.txt
  • a_STAVOSHFM.txt1_ACTUAL_AP032015_RR.txt
  • a_STAVOSHFM.txt1_ACTUAL_AP052016_RR.txt
  • a_STAVOSHFM.txt1_ACTUAL_AP062015_RR.txt
  • a_STHCPSHFM.txt1_ACTUAL_AP022016_RR.txt
  • a_STHCPSHFM.txt1_ACTUAL_AP032015_RR.txt
  • a_STHCPSHFM.txt1_ACTUAL_AP052016_RR.txt
  • a_STHCPSHFM.txt1_ACTUAL_AP062015_RR.txt

Basically the only issue to resolve is the removing of the .txt1 from the inserted file name. The expected result would be:
Code:
  • a_STAVOSHFM_ACTUAL_AP022016_RR.txt
  • a_STAVOSHFM_ACTUAL_AP032015_RR.txt
  • a_STAVOSHFM_ACTUAL_AP052016_RR.txt
  • a_STAVOSHFM_ACTUAL_AP062015_RR.txt
  • a_STHCPSHFM_ACTUAL_AP022016_RR.txt
  • a_STHCPSHFM_ACTUAL_AP032015_RR.txt
  • a_STHCPSHFM_ACTUAL_AP052016_RR.txt
  • a_STHCPSHFM_ACTUAL_AP062015_RR.txt

# 13  
Old 12-20-2016
You did NOT copy the proposal. How do you expect it to work, then?
# 14  
Old 12-20-2016
I did add the proposed changes if I leave the curly bracket before the declaration of the temporary variable it does not change the file name. If I place it before the print command, it adds a 1 to the text extension; right before the word actual.
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";} 
TMP = FILENAME sub (/\.*$/, "", TMP) 
{print > "a_" TMP "_ACTUAL_"months[substr($3,0,3)]"20" substr($3,5,2)"_RR.txt"}' *.txt

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