Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append contents of file A to the end of each line in file B? bash

Tags:

bash

shell

sed

awk

I really can't get this one.

File A has this:

1.1.1.1
2.2.2.2
3.3.3.3

etc..

File B will always have the exact same amount of lines and they always will correspond:

oneoneoneone
twotwotwotwo
3ee3ee3ee3ee

I want to append file A to file B so it looks like:

1.1.1.1 oneoneoneone
2.2.2.2 twotwotwotwo
3.3.3.3 3ee3ee3ee3ee

This is what I have but not working like it should:

for z in `cat /tmp/fileB; do sed "s/(.*)/\1$z/" < /tmp/fileA >> /tmp/c;done

Any suggestions?

like image 214
user172643 Avatar asked Dec 12 '25 05:12

user172643


1 Answers

If you want to append the lines in fileB to the lines in fileA (as indicated by your desired output), you can simply do:

paste fileA fileB

That uses a tab for the delimeter, so you might prefer

paste -d ' ' fileA fileB

If you want to do it with awk, you can do:

awk '{ getline b < "fileB"; print $0, b}' fileA

This may be possible with sed, but it's not advisable. Similar to what you seem to be trying with the loop, you can also do:

while read b; do read -u 4 a; echo $a $b; done < fileb 4< filea
like image 117
William Pursell Avatar answered Dec 14 '25 06:12

William Pursell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!