Register
It is currently Wed Apr 23, 2014 1:34 pm

find and replace a string with sed [SOLVED]


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon Dec 20, 2010 9:50 am   

Joined: Mon Nov 08, 2010 10:39 am
Posts: 17
Hi,
I have a file which contains usernames, number of them:
Code:
username1: aaaa
user2: bbbb2
useradmin3: ccccd

I need for example to find the string (we do not know what the text is after colon):
Code:
user2: anything
and replace with
Code:
user2: root

I am trying to use something like this:
Code:
sed -i 's/user2: */user2: root/g' file

but of course it's not working.
I googled some time and still can't find solution.
Thank you.


Last edited by lush on Tue Dec 28, 2010 3:41 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon Dec 20, 2010 12:57 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi Lush!

I sure hope you've found an answer to this by now, but I think I see the problem. In your 'sed' command, the search string you're using is 'user2: *'. What this matches is 'user2:' followed by any number of spaces. Using your example, the result is that it ADDS 'root' in before the existing data:

Code:
$ cat > test.txt
username1: aaaa
user2: bbbb2
useradmin3: ccccd

$ sed 's/user2: */user2: root/' test.txt
username1: aaaa
user2: rootbbbb2
useradmin3: ccccd


So, if what you really want to do is replace that entire line, you have to use the "." wildcard. The dot means "Any Single Character". The asterisk doesn't behave like you'd expect it to in regular expressions. It means that the character before it can repeat until infinity and it'll keep matching it.

So in your example, you're matching an infinite number of "spaces" with " *". To match ANY character, not just spaces, you'll want to use the dot.

Code:
$ sed 's/user2: .*/user2: root/' test.txt
username1: aaaa
user2: root
useradmin3: ccccd


That should give you the result you're looking for. I hope this helps!

-Jeo


Top
 Profile YIM  
 PostPosted: Mon Dec 20, 2010 10:15 pm   

Joined: Mon Nov 08, 2010 10:39 am
Posts: 17
Thank you so much, Jeo!
Especially for the detailed explanation for newbie)) That's appreciated.


Top
 Profile  
 PostPosted: Sat Dec 25, 2010 4:36 am   

Joined: Mon Nov 08, 2010 10:39 am
Posts: 17
Could you guide me how to pass a variable to the sed regular expression? Like:
Code:
sed 's/$user2: .*/$user2: root/' test.txt
I tried different ways. As to the topic:
http://www.unix.com/shell-programming-scripting/23292-passing-variables-sed-inside-script.html
they solve this with awk. Any ways to code the expression with sed?


Top
 Profile  
 PostPosted: Mon Dec 27, 2010 5:28 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Just do not use single quotes on your variables:
Code:
sed 's/'$user2': .*/'$user2': root/' test.txt

If $user2 can contain some weird characters (spaces, dots, $, etc.), you can add double quotes around it.


Top
 Profile  
 PostPosted: Tue Dec 28, 2010 3:40 pm   

Joined: Mon Nov 08, 2010 10:39 am
Posts: 17
That's it. Thank you very much.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP