1

files have many entries but for sample posting only 2 entries each. I want to merge file1 first line content with file 2's 1st line content and so on.example is as below.

file1.txt

Start Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
Start Date: 2019-01-08 04:16:57; End Date: 2019-01-08 04:17:02; Difference in seconds = 5

file2.txt

Fnb_dm__exachange_rollback_character
dnb_exchange_brm_rollback_character

I want output like below:

Fnb_dm__exachange_rollback_character :Start Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
dnb_exchange_brm_rollback_character :Start Date: 2019-01-08 04:16:57; End Date: 2019-01-08 04:17:02; Difference in seconds = 5

How to do it using ksh?

4
  • 1
    Do you specifically need the delimiter to be the two-character string ` :` (space-colon)? if not you could use something as simple as paste -d: file2.txt file1.txt Commented Jan 19, 2019 at 15:34
  • @stelldriver : no specific requirement about delimiter Commented Jan 19, 2019 at 15:36
  • @Pankaj -- no no no; open a new question -- don't move the goalposts on this one!
    – Jeff Schaller
    Commented Jan 19, 2019 at 17:18
  • @JeffSchaller:will do that Commented Jan 19, 2019 at 17:31

3 Answers 3

3

You can use the paste command - for example, to merge the files line-by-line with a colon delimiter:

$ paste -d: file2.txt file1.txt
Fnb_dm__exachange_rollback_character:Start Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
dnb_exchange_brm_rollback_character:Start Date: 2019-01-08 04:16:57; End Date: 2019-01-08 04:17:02; Difference in seconds = 5

If you need a multi-character delimiter, then that is possible using pr ex.

$ pr -TmJS' :' file2.txt file1.txt
Fnb_dm__exachange_rollback_character :Start Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
dnb_exchange_brm_rollback_character :Start Date: 2019-01-08 04:16:57; End Date: 2019-01-08 04:17:02; Difference in seconds = 5
2
  • it worked ,thanks a lot,Actully I want this file to be sorted on last field I,e example (51), actually I knw how to sort but for that I have to awk that colum then only I can sort (which will give me only last filed in result)but I want to keep all data on that line also, so how can I achive this? Commented Jan 19, 2019 at 16:53
  • sorting wasn't one of this question's requirements; I'd suggest that you open a new question with steeldriver's current solution and your further requirements.
    – Jeff Schaller
    Commented Jan 19, 2019 at 17:15
0

I have done by below method and it worked fine

i=`awk 'NR==1{print $0}' file1`;awk -v i="$i" 'NR==1{$0=$0i}1' file2

output

Fnb_dm__exachange_rollback_characterStart Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
dnb_exchange_brm_rollback_character
1
  • shouldn't the dnb_exchange_brm_rollback_character line have more fields next to it?
    – Jeff Schaller
    Commented Jan 20, 2019 at 13:29
0

I have done by below method and it worked fine

for i in 1 2; do j=`sed -n ''$i'p' file1`;sed -n ''$i'p' file2|sed "s/.*/&$j/g"; done

output

Fnb_dm__exachange_rollback_characterStart Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
dnb_exchange_brm_rollback_characterStart Date: 2019-01-08 04:16:57; End Date: 2019-01-08 04:17:02; Difference in seconds = 5
1
  • I suppose you should explain that your solution works only for the stripped-down example; from the OP: files have many entries but for sample posting only 2 entries each
    – Jeff Schaller
    Commented Jan 20, 2019 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .