Joining Similarly Named Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining Similarly Named Files
# 1  
Old 10-03-2012
Joining Similarly Named Files

Have a few hundred files that I have no control over. I need to join them on a common string. There are two files per unit. The file names are:
Code:
AAAA_X_file1.txt
AAAA_file2.txt

join -a1 file1 file2 - this works.

My question is, is there a way to use the join command listing the file names only?

Last edited by Franklin52; 10-05-2012 at 04:58 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-03-2012
This is confusing to me.

Do you want to join 200 files into 100 files or 1 big one?

And yes it is possible to feed a list of filenames into a command, a string of piped commands or a shell script.

Please give us a sample of your input file names and the expected output
# 3  
Old 10-03-2012
Thanks Jim. Yes, the question is not clear.
I want to make one large file out of the 200. There is a common field to join and the command works nicely:

Code:
join -a1 AAAA_X_file1.txt AAAA_file2.txt > desired_output
join -a1 BBBB_X_file1.txt BBBB_file2.txt >> desired_output
join -a1 CCCC_X_file1.txt BBBB_file2.txt >> desired_output

My question is, is there a way to use the join command say with wild cards so that instead of having to use the join command 100 times?

Last edited by Franklin52; 10-05-2012 at 04:59 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 10-04-2012
No you have to run it 100 times in a loop
Create an input file of pairs of names as indicated by your example:
Code:
ls files* | awk 'NR%2==0 {print ""} {printf("%s ", $0)} END{print ""} ' > pairfile
> exportfile
while read a b
do
 join -a1 $a $b
done <pairfile >exportfile

# 5  
Old 10-04-2012
Thanks Jim. This looks like the answer but I may have listed my files incorrectly. The files are named like this:
Code:
AAAA_X_X.txt
AAAA_X.txt

So this line does work.
Code:
ls files* | awk 'NR%2==1 {print ""} {printf("%s ", $1)} END{print ""} '

---------- Post updated at 03:32 PM ---------- Previous update was at 11:48 AM ----------

Sorry! I meant this line does not work.
Code:
ls files* | awk 'NR%2==1 {print ""} {printf("%s ", $1)} END{print ""} '


Last edited by Franklin52; 10-05-2012 at 04:59 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help me in joining two files

I have two files with the below contents : sampleoutput3.txt 20150202;hostname1 20150223;hostname2 20150716;hostname3 sampleoutput1.txt hostname;packages_out_of_date;errata_out_of_date; hostname1;11;0; hostnamea;12;0; hostnameb;11;0; hostnamec;95;38; hostnamed;440;358;... (2 Replies)
Discussion started by: rahul2662
2 Replies

2. Shell Programming and Scripting

Joining 2 Files

File "A" (column names: Nickname Number GB) Nickname Number GB PROD_DB0034 100A 16 ASMIL1B_DATA_003 100B 16 PSPROD_0000 1014 36 PSPROD_0001 100D 223 ..... File "B" (column names: TYPE DEVICE NUMBER SIZE) TYPE DEVICE NUMBER SIZE 1750500 hdisk2 100A 16384 1750500 hdisk3 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

3. Shell Programming and Scripting

Help with joining files and adding headers to files

Hi, I have about 20 tab delimited text files that have non sequential numbering such as: UCD2.summary.txt UCD45.summary.txt UCD56.summery.txt The first column of each file has the same number of lines and content. The next 2 column have data points: i.e UCD2.summary.txt: a 8.9 ... (8 Replies)
Discussion started by: rrdavis
8 Replies

4. Shell Programming and Scripting

Joining two files into one

Hi experts, I'm quite newbie here!! I have two seperate files. Contents of file like below File 1: 6213019212001 8063737 File:2 15703784 I want to join these two files into one where content will be File 3: 6213019212001 8063737 15703784 Regards, Ray Seilden (1 Reply)
Discussion started by: RayanS
1 Replies

5. Shell Programming and Scripting

Joining Three Files

Hi guys, I have three files which needs to be joined to a single file. File 1: Col a, Col b, Col c File 2: Col 1a, Col 1b File 3: Col 2a, Col 2b Output: Col 1a, Col 2a, Col a, Col b, Col c. All the files are comma delimited. I need to join Col b with Col 1b and need to... (17 Replies)
Discussion started by: mac4rfree
17 Replies

6. Shell Programming and Scripting

cat certain files in directories to files named after the dir?

Hi all, I have a directory with many subdirectories each named like so: KOG0001, KOG0002, ...KOG9999. Each of these subdirectories contain a variable number two kinds of files (nuc and prot) named like so: Capitella_sp_nuc_hits.fasta (nuc) and Capitella_sp_prot_hits.fasta (prot). The... (2 Replies)
Discussion started by: kmkocot
2 Replies

7. Shell Programming and Scripting

Joining files

Hi, Whats the unix function to join multiple files? is it cat? so I have multiple files in the same format and I want to join then by row eg. FILE1 1 3 1 3 1 3 1 3 FILE2 2 4 2 4 2 4 (1 Reply)
Discussion started by: kylle345
1 Replies

8. Shell Programming and Scripting

Help with joining two files

Greetings, all. I've got a project that requires I join two data files together, then do some processing and output. Everything must be done in a shell script, using standard unix tools. The files look like the following: File_1 Layout: Acct#,Subacct#,Descrip Sample: ... (3 Replies)
Discussion started by: rjlohman
3 Replies

9. UNIX for Dummies Questions & Answers

joining files

Hi, Could anyone help me ? I'm trying to join two files, but no common field are on them. So I think on generate \000\ sequence to add for each line on both files, so then will be able to join these files. Any idea? Thanks in advance, (2 Replies)
Discussion started by: Manu
2 Replies

10. UNIX for Dummies Questions & Answers

joining 2 files

Hi, I have two files that I need to find difference between. Do I use diff or join? If join, how do I use it? thanks, webtekie (1 Reply)
Discussion started by: webtekie
1 Replies
Login or Register to Ask a Question