LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   modify entries in a list of names :: should be easy enough, right? (https://www.linuxquestions.org/questions/linux-newbie-8/modify-entries-in-a-list-of-names-should-be-easy-enough-right-864067/)

linux_evangelist 02-21-2011 11:09 AM

modify entries in a list of names :: should be easy enough, right?
 
I would like to take a list of names in a file and append text to the beginning and end of them.

eg.

=========
BEFORE
=========
cat ./names.txt
jade
joe
john
harry

=========
AFTER
=========
cat ./names.txt
email: jade@somedomain.com
email: joe@somedomain.com
email: john@somedomain.com
email: harry@somedomain.com

druuna 02-21-2011 11:15 AM

Hi,

Code:

sed 's/\(.*\)/email: \1@somedomain.com/' names.txt
This uses back-referencing: All between \( and \) in the search part can be represented by \1 in the replace part.

Hope this helps.

Add the -i flag to make changes in place.

soplin 02-21-2011 11:21 AM

paste names.txt email.txt // puts texts side by side james email@example.com

Maybe you meant cat email.txt >> names.txt which will append email.txt after names.txt

james
email@example.com

Medievalist 02-21-2011 02:34 PM

I'd use GNU awk for that, although sed will work fine.
 
If you don't have the gnu awk, install it. It's a nuclear powered chainsaw for text processing.

gawk '{print $1 "@somedomain.com"}' names.txt

ought to do the job for you...

druuna 02-21-2011 02:38 PM

Hi,
Quote:

Originally Posted by Medievalist (Post 4266252)
gawk '{print $1 "@somedomain.com"}' names.txt

ought to do the job for you...

It does not, this does:
Code:

gawk '{print "email: " $1 "@somedomain.com"}' names.txt
@soplin: both your solutions don't work......

@linux_evangelist: If this is solved can you put up the [SOLVED] flag (First post -> Thread Tools).

Hope this helps.

soplin 02-21-2011 03:06 PM

Now I understand what linux_evangelist meant. jade => Email: jade@somedomain.com
Definitely gawk or sed.

If you did have a file with "Email: ", another with "jade" and another with "@somedomain.com", paste would work but not like the gawk or sed example posted above. paste -d "" file1 file2 file3 > file123. cat file123 would result in
Email: jade@somedomain.com .

Medievalist 02-21-2011 03:36 PM

Thank you druuna for the correction
 
Yes, druuna is right (thanks!) I misread the original post slightly and left off the prefix.

Code:

gawk '{print "email: " $1 "@somedomain.com"}' names.txt
Another way that works equally well is

Code:

gawk '{printf "email: %s@somedomain.com\n", $1}' names.txt
Note you have to supply the newline if you use printf instead of print.

I like gawk better than sed for this kind of problem because it's easier for me to read and figure out months or years later. Unix sed is fast and effective, but it has such a terse syntax that it's hard to do anything complex in it without creating alphabet soup!

linux_evangelist 02-22-2011 07:50 AM

thanks guys.


All times are GMT -5. The time now is 03:13 PM.