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]

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.

  • 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 '16 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 '16 at 14:50
  • @Jacky This might be what you're looking to do. stackoverflow.com/questions/3806874/… – jbrock Jun 4 '16 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 '16 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\njohnny@email.com" (\n means new line) – InitializeSahib Jun 13 '16 at 20:33
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
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
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: johnny@email.com
User: james Email: james@email.com
User: clara Email: clara@email.com
User: brandon Email: brandon@email.com
User: steve Email: steve@email.com
User: louis Email: louis@email.com
User: daniele Email: daniele@email.com
  • 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 '16 at 14:46

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.