Concatenate files based on names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate files based on names
# 1  
Old 06-27-2016
Concatenate files based on names

Dear all,
I have a list of files and I woulk like to concatenate some of them based on their name.
Basically my files are names like that:

Code:
file1_abcd_other_useless_letters_1_C1.txt
file1_abcd_other_useless_letters_1_C2.txt
file1_xywz_other_useless_letters_1_C1.txt
file1_xywz_other_useless_letters_1_C2.txt
file2_abcd_other_useless_letters_2_C1.txt
file2_abcd_other_useless_letters_2_C2.txt
file2_xywz_other_useless_letters_2_C1.txt
file2_xywz_other_useless_letters_2_C2.txt

Basically, I woul like the files with the same color to be concatenate.
Output:
Code:
abcd_other_useless_letters_C1.txt (merge of file1_abcd_other_useless_letters_1_C1.txt & file2_abcd_other_useless_letters_2_C1.txt )
abcd_other_useless_letters_C2.txt
xywz_other_useless_letters_C1.txt
xywz_other_useless_letters_C2.txt

That means (suppose
Code:
_

as separator) ,for the merged files

1)first field
Code:
file

should be different
2) second field
Code:
abcd

should be the same
3) fields
Code:
other_useless_letters

should not be considered at all
4) second to last field
Code:
1

should be different
5) last field
Code:
C1

should be the same

well, I think that the color code of file is still the better explanation

Hope someone can help me.

Best

Giuliano
# 2  
Old 06-27-2016
Would this do (not too old bourne type shell required)?

Code:
ls f*.txt |
while read FN
  do    IFS="_." read X P1 O1 U1 L1 N1 P2 EX <<< $FN
        NFN="${P1}_${O1}_${U1}_${L1}_${P2}.${EX}"
        touch $NFN
        cat $FN >> $NFN
  done

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-27-2016
It works great!

Thanks!
# 4  
Old 06-27-2016
As you may have noticed, there is no check for the number in the second last field to be different. Should that be compulsory, we'd need to insert some additional processing (and mayhap another new file name definition).
# 5  
Old 06-27-2016
Actually that number is useless so is fine like that...it's always different if first field is different..

Thanks

Giuliano
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to concatenate the files based upon the file name?

Hi Experts, I am trying to merge multiple files into one file based upon the file name. Testreport_Server1.txt ============ MonitoringReport_Server1.txt============ CentralReport_Server1 Here two files containing server1 should be merged into one file? How can i do... (16 Replies)
Discussion started by: sharsour
16 Replies

2. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

3. Shell Programming and Scripting

Sorting file based on names

Hi I have some files in directory and the names of files are like jnhld_15233_2010-11-23 jnhld_15233_2007-10-01 jnhld_15233_2001-05-04 jnhld_15233_2011-11-11 jnhld_15233_2005-06-07 jnhld_15233_2000-04-01 ..etc How can i sort these files based on the date in the file name so that ... (4 Replies)
Discussion started by: morbid_angel
4 Replies

4. Shell Programming and Scripting

Archive different folders based on their names

This is my first post so ... be gentle:) Hello I have several folders that are backed up daily in following format: /back_YY.MM.DD/backup1/* ........................./backup2/* I looking a script to archive and rename all backup folders bazed on root folder... (8 Replies)
Discussion started by: vilibit
8 Replies

5. Shell Programming and Scripting

want to concatenate multiple files based on the rest of ls -lrt

uadm@4132> ls -lrt -rw------- 1 uadm uadm 3811819 Jun 6 04:08 data_log-2010.05.30-10:04:08.txt -rw------- 1 uadm uadm 716246 Jun 13 01:38 data_log-2010.06.06-10:04:08.txt -rw------- 1 uadm uadm 996 Jun 13 04:00 data_log-2010.06.06-10:04:22.txt -rw------- 1 uadm uadm 7471 Jun 20 02:03... (5 Replies)
Discussion started by: mail2sant
5 Replies

6. Shell Programming and Scripting

Concatenate multiple lines based.

Hello, I have been searching the forum for concatenation based on condition. I have been close enough but not got th exact one. infile: -----DB_Name ABC (X, Y,Z). DB_Name DEF (T). DB_Name GHI (U ,V,W). Desired Output file should be: ---------------------------DB_Name ABC... (8 Replies)
Discussion started by: indrajit_u
8 Replies

7. Shell Programming and Scripting

Adding lines to files based on their names

I have several files on a folder something like this: If the file starts with 1a, I would like to add the following at the very beggining of the file If the file starts with 1b then I should add the following For files starting with 6 So on and so forth. Thus, at the end each file will... (18 Replies)
Discussion started by: Xterra
18 Replies

8. Shell Programming and Scripting

Insert file names when concatenate files into a file

Hi I found the following line would concatenate all test_01 test_02 test_03 files into "bigfile". cat test_* >> bigfile But, what I'm looking for a way to insert each file names in order when concatenated in "bigfile". Thank you samky2005 (2 Replies)
Discussion started by: samky2005
2 Replies

9. Shell Programming and Scripting

Separate based on file names

Hello experts, This might prove to be a stupid question to some of you, but I have tried to tackle it in different ways. Being new to shell scripting, I am requesting your help in coming up with an elegant solution. I am using Korn shell. We have a directory with file names with the pattern:... (2 Replies)
Discussion started by: prashk15
2 Replies

10. UNIX for Dummies Questions & Answers

File names based off of wildcards

Hello all- First post, so just to forewarn you: I know enough about Perl and the Terminal to get myself into trouble, not quite enough to always get out. I'd like to know if it is possible to, from the command prompt, use a wild-card to declare the names of files for input and then use the... (1 Reply)
Discussion started by: tcquad
1 Replies
Login or Register to Ask a Question