1

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]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

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

5
  • Are these exact examples? There isn't a reason to have two files, if so. The name can be simply pulled from the email.
    – user508889
    Jun 4, 2016 at 11:29
  • You're right. I do a better example. What i really need is merge two files (one with name of websites and the other file with relative link) and saving in one output with link below his name. (sorry my bad english but is not my mother-language) ;)
    – Jacky
    Jun 4, 2016 at 14:50
  • @Jacky This might be what you're looking to do. stackoverflow.com/questions/3806874/…
    – jbrock
    Jun 4, 2016 at 17:13
  • Can you clarify what you want to do, that is, what happens for "add any username of 1st file his mail from 2nd file"?
    – Scooter
    Jun 7, 2016 at 4:53
  • @Scooter I believe they mean that they want the e-mail for that user to be added underneath their username. Like: "johnny" becomes "johnny\[email protected]" (\n means new line) Jun 13, 2016 at 20:33

2 Answers 2

2

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
[email protected]


james
[email protected]


clara
[email protected]


brandon
[email protected]


steve
[email protected]


louis
[email protected]


daniel
[email protected]
1

If you only need one file, mails.txt in your example, here is a simple script.

It is meant to be ran by specifing the file to your emails. ./script.sh mails.txt

script.sh

#!/bin/bash
filename="$1"
while read -r line
do
name=${line%@*}
echo "User: ${name} Email: ${line}"
done < "$filename"

Output

bcollins@home:~/t$ ./script.sh mails.txt 
User: johnny Email: [email protected]
User: james Email: [email protected]
User: clara Email: [email protected]
User: brandon Email: [email protected]
User: steve Email: [email protected]
User: louis Email: [email protected]
User: daniele Email: [email protected]
1
  • Thanks bc2946088. But i need a general rule (like a script or simply commands) for any case to add link below his name (name: video and down: vimeo.com etc...). Can you suggest another idea?
    – Jacky
    Jun 4, 2016 at 14:46

You must log in to answer this question.

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