LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-21-2011, 11:09 AM   #1
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Rep: Reputation: 0
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
 
Old 02-21-2011, 11:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.

Last edited by druuna; 02-21-2011 at 11:16 AM. Reason: Added -i flag info
 
1 members found this post helpful.
Old 02-21-2011, 11:21 AM   #3
soplin
Member
 
Registered: Apr 2007
Distribution: Mostly Debian
Posts: 114

Rep: Reputation: 19
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

Last edited by soplin; 02-21-2011 at 11:29 AM.
 
Old 02-21-2011, 02:34 PM   #4
Medievalist
Member
 
Registered: Aug 2003
Distribution: Dead Rat
Posts: 175

Rep: Reputation: 37
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...
 
1 members found this post helpful.
Old 02-21-2011, 02:38 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,
Quote:
Originally Posted by Medievalist View Post
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.

Last edited by druuna; 02-21-2011 at 02:40 PM.
 
1 members found this post helpful.
Old 02-21-2011, 03:06 PM   #6
soplin
Member
 
Registered: Apr 2007
Distribution: Mostly Debian
Posts: 114

Rep: Reputation: 19
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 .

Last edited by soplin; 03-16-2011 at 07:03 PM.
 
Old 02-21-2011, 03:36 PM   #7
Medievalist
Member
 
Registered: Aug 2003
Distribution: Dead Rat
Posts: 175

Rep: Reputation: 37
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!

Last edited by Medievalist; 02-21-2011 at 03:37 PM. Reason: removed spurious carriage return in code block
 
Old 02-22-2011, 07:50 AM   #8
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Original Poster
Rep: Reputation: 0
thanks guys.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List 4 names from users list and output them to fbusers in numbered ascending order? fezzie Programming 4 02-10-2010 01:05 PM
modify owner and group names when copying on linux bash w3developing Linux - Newbie 5 08-14-2009 09:32 PM
How to Make Modify the Entries in Access Control Database for Linux Kernel 2.4.18-14? kks_gs Linux - Security 1 02-07-2009 06:31 AM
LXer: Use PHP to create & modify Active Directory/LDAP entries LXer Syndicated Linux News 0 01-01-2009 12:30 AM
In Linux, How to read and modify Kernel IP routing table entries from a c program sakthivelbatusai Linux - Networking 1 03-29-2007 06:36 AM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration