0

I have multiple files like this:

BOB_1.brother_bob12.txt
BOB_2.brother_bob12.txt
..
BOB_35.brother_bob12.txt

How to join these files in order from {1..36} and append filename at the end of each row? I have tried:

for i in *.txt; do sed 's/$/ '"$i"'/' $i; done > outfile #joins but not in order
cat $(for((i=1;i<38;i++)); do echo -n "BOB_${i}.brother_bob12.txt "; done) # joins in order but no filename at the end

file sample:

1 345 378 1 3 4 5 C T
1 456 789 -1 2 3 4 A T
user3224522
  • 1,119
  • 8
  • 19
  • 1
    :D `append filename at the end of each row?` so like you want the first line in output to be `1 345 378 1 3 4 5 C T BOB_1.brother_bob12.txt` ? – KamilCuk Jan 14 '21 at 10:31
  • exactly what I want to do :) – user3224522 Jan 14 '21 at 10:32
  • 1
    `joins but not in order` - it would be in order, but in order like `1` `10` `11` ... `19` `2` `20` `21` ... etc., because like `2_` is sorted after `10`. – KamilCuk Jan 14 '21 at 10:36

3 Answers3

2

Do not do cat $(....). You may just:

for ((i=1;i<38;i++)); do 
     f="BOB_${i}.brother_bob12.txt"
     sed "s/$/ $f/" "$f"
done

You may also do:

printf "%s\n" bob.txt BOB_{1..38}.brother_bob12.txt |
     xargs -d'\n' -i sed 's/$/ {}/' '{}'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • could you please explain so that I can modify if needed... for example if I want to add a file named differently. Thnaks! – user3224522 Jan 14 '21 at 10:36
  • 1
    Well, there is `'s/$/ '"$i"'/' $i` in your code also, it does the same thing. Just the loop is different. Maybe look here: https://stackoverflow.com/questions/15978504/add-text-at-the-end-of-each-line and read some sed introduction. For learning regex, I recommend regex crosswords, they are amazing and fun. Which part exactly you do not understand? To add a filename differently , modify the `"s/$/ $f/"` command, but for anything more complicated `awk` may be more suitable. – KamilCuk Jan 14 '21 at 10:37
  • Is it posssible to add a file with different name before all bob file and how? – user3224522 Jan 14 '21 at 10:57
  • What name exactly? You may edit with `s/$/ different_name_before_bob_BOB_${i}/`. – KamilCuk Jan 14 '21 at 11:09
  • it is file named bob.txt that should go at the beginning before all other files.. – user3224522 Jan 14 '21 at 11:11
  • Soooo just add before it `sed "s/$/ bob.txt/" "bob.txt"`? – KamilCuk Jan 14 '21 at 11:23
  • Please be specific. If your file is not `bob.txt` but `/the/path/to/bob.txt` then you will get such error. Consider using a different `s` separator, like `s~pattern~replacement~` (any character may be used) like `s@$@ bob.txt@`. For more information, research `sed` `s` command. – KamilCuk Jan 14 '21 at 11:29
1

You may use:

for i in {1..36}; do
   fn="BOB_${i}.brother_bob12.txt"
   [[ -f $fn ]] && awk -v OFS='\t' '{print $0, FILENAME}' "$fn"
done > output

Note that it will insert FILENAME as the last field in every record. If this is not what you want then show your expected output in question.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

This might work for you (GNU sed);

sed -n 'p;F' BOB_{1..36}.brother_bob12.txt | sed 'N;s/\n/ /' >newFile

Used 2 invocations of sed, the first to append the file name after each line of each file. The second to replace the newline between each 2 lines by a space.

potong
  • 55,640
  • 6
  • 51
  • 83