-1

I am working in bash (Fedora). I have a list of text files as follows:

filenameA   filename1
filenameA   filename2
filenameA   filename3
filenameB   filename4
filenameB   filename5

I wish to combine (cat) all of the files listed in second column with their match in the first column like this:

cat filenameA filename1 filename2 filename3  > newfilenameA 
cat filenameB filename4 filename5 > newfilenameB

Is there a 'while' or 'for' loop that can accomplish this for a longer list of files? I want the file contents to be combined, not just a list of file names.

Trevor
  • 1
  • 2

1 Answers1

3

I would suggest that you used awk:

awk '{a[$1]=a[$1] OFS $2}END{for(i in a)system("cat " i OFS a[i] " >new" i)}' file

The first block groups the entries based on the value of the second column. The entries are separated by OFS, the Output Field Separator, which is a space by default.

In the END block, once all of the lines in the file have been processed, the loop goes through each element of a, using system to run cat in the shell. In awk, strings are concatenated by default, so the command string can be built by joining together the array key i and the value a[i]. The output of cat is redirected to a file, the name of which is obtained by prefixing the input file name with "new".

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • I was unclear re. desired output. I wish for file contents to be combined. The solution above gives me a new file with "filename filename" but not the file contents. Thanks! – Trevor Oct 06 '14 at 16:29
  • The code in the edit gave me - awk cmd syntax error - "unterminated string" at the final quote. I think it is missing a quote. – Trevor Oct 06 '14 at 16:56
  • It runs perfectly, except I have to ctrl-c to end the hangup at the end. – Trevor Oct 06 '14 at 17:25
  • That's probably because there are some blank lines in your input. It would be possible to work around this if necessary, either by adding in a check into the script or removing the blank lines. – Tom Fenech Oct 06 '14 at 17:32