Ubuntu – Add lines from file to another file

command linetext processing

I have two files.

First file (users.txt):

!/bin/bash/

johnny

james

clara

brandon

steve

louis

daniel

Second file (mails.txt)

[The first line is not empty, the file contains only mail address without spaces]

johnny@email.com

james@email.com

clara@email.com

brandon@email.com

steve@email.com

louis@email.com

daniele@email.com

How can I add under any username of 1st file his mail from 2nd file?
I think the sed command is needed.

Best Answer

You can read both files together line by line to get your desired output. ( I assume you don't have any other unwanted lines in these files )

while read -r line1 && read -r line2 <&3;
do
    echo $line1
    echo $line2

done<users.txt 3<mails.txt

users.txt is read using standard input file descriptor 0

mails.txt is read using our given file descriptor 3

Output:

johnny
johnny@email.com


james
james@email.com


clara
clara@email.com


brandon
brandon@email.com


steve
steve@email.com


louis
louis@email.com


daniel
daniele@email.com