The paste
command could be used to pair the lines of the two files so that we may read them easier:
paste user.txt contact.txt
This creates a tab-delimited data stream of two fields.
This is easily read and modified by e.g. awk
:
awk -F '\t' '{ printf "user name is %s\nand his mobile number is %s\n\n", $1, $2 }'
This uses printf
in awk
to create a string that is outputted. The two fields read from the output of paste
are read automatically into $1
and $2
and they are inserted into the output string where it says %s
(a placeholder for the next string given as an argument to printf
). The -F '\t'
on the command line of awk
sets the field delimiter to a tab character.
Putting these two together and testing it:
$ paste user.txt contact.txt | awk -F '\t' '{ printf "user name is %s\nand his mobile number is %s\n\n", $1, $2 }'
user name is apple
and his mobile number is 1234
user name is cat
and his mobile number is 3456
user name is tom
and his mobile number is 5678
Redirect this into a file using >somename
at the end of the command.
A different way of doing this is to modify the output field separator (OFS
) and the output record separator (ORS
), and instead of using printf
just modify the first and second field (the trailing 1
at the end causes the output of the modified line):
paste user.txt contact.txt |
awk -F '\t' -v OFS='\n' -v ORS='\n\n' '{ $1 = "user name is " $1; $2 = "and his mobile number is " $2 }; 1'