LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-30-2010, 02:58 AM   #1
lewkh
Member
 
Registered: Oct 2004
Posts: 69

Rep: Reputation: 15
Regular Expression


Hi Folk,

I got regular expression question. e.g my string like below

admin:x:111:tester1

How do I replace as admin:x:111:tester1,tester2 in sed?

What is the regular expression syntax to be?

Please help!

Thanks
 
Old 04-30-2010, 03:28 AM   #2
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Code:
sed -i 's/\(admin:x:111:tester1\)/\1,tester2' file.txt
 
Old 04-30-2010, 03:35 AM   #3
lewkh
Member
 
Registered: Oct 2004
Posts: 69

Original Poster
Rep: Reputation: 15
Hi rkski,

Thanks for your reply. I found that "s/admin.*/&,tester2/" work also. Could I know what is the different?
 
Old 04-30-2010, 04:01 AM   #4
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
the '&' symbol will copy the matched text into the substitution.
your regex (admin.*) will match all lines containing 'admin' followed by any text and add ",tester2" to the end of that.

ie. yours is a more general match. If there's only one occurance of 'admin' and the format of the file lines is similar to what you have given, the end result is the same.

HTH

edit: if there's a line:
Quote:
sys:x:3:admin
yours will change that too (which you probably don't want):
Quote:
sys:x:3:admin,tester2
with your way make a slight change:
Quote:
"s/^admin.*/&,tester2/"

Last edited by rkski; 04-30-2010 at 04:27 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what does this regular expression do? smeezekitty Programming 10 10-07-2009 07:12 PM
regular expression (.*?) uttam_h Programming 6 05-30-2008 06:45 PM
Need help with a regular expression abefroman Programming 2 05-28-2008 10:17 PM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 11:08 AM
Regular Expression slizadel Programming 4 07-28-2003 06:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration