3

I have two files. file1 contains text as follows:

#This Data written by the platform Team
id=kingDB

file2 contains the text as follows:

#This Data written by the platform Team
id=kiteDB,mangoDB

I want to merge the file1 data in file2, Afterwards file2 needs to be as follows:

#This Data written by the platform Team
id=kiteDB,mangoDB,kingDB

How can I do that?

  • Is it always one line? Are there only comments at the top of the files (or between lines if there are multiple). If multiple lines, are the 'id=' parts unique? – Anthon Nov 2 '14 at 7:06
  • there is only two lines in each files,first line will be same comment and second have id common,only id= .. text will be different,so from file1 need to add id Text if differ from file2 by separating comma. – Sachin Nov 2 '14 at 7:22
3

Usual way to merge to files is join

join -t = file{1,2} | sed s/=/,/2
| improve this answer | |
  • thanks all, I have tried with Anthon solutions its working fine – Sachin Nov 3 '14 at 10:03
1

You can use sed, tail and cut:

 sed -i "s/=\(.*\)/=\1,$(tail -1 file1 | cut -d = -f 2)/" file2

The commands between $(...) first take the last line of file1, cuts it based on the = character and takes the seconds field (kingDB), then it puts that at the end of the line with '=' of the second file.¹

Not all sed versions have -i, if yours doesn't try:

sed "s/=\(.*\)/=\1,$(tail -1 file1 | cut -d = -f 2)/" file2 > file2.tmp
mv -f file2.tmp file2

¹ I am assuming here that file2 did end in a newline, if not you can do without sed

| improve this answer | |
0

If you have single line you can use the following code:

echo "id=" $(paste -d "" <( awk -F',' {'printf "%s,%s,", $1, $2  '} file2 ) <( awk -F'=' {'print $2'} file1 ) <(sed -i 's/id=//g' file2 )) > file2
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.