Register
It is currently Tue Jul 22, 2014 11:44 pm

Need help with sed command


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sun Sep 11, 2011 12:00 am   

Joined: Sat Sep 10, 2011 11:48 pm
Posts: 7
Hello everybody
I have a file temp.tmp which contains the following:

1:0
2:0
3:0
4:0
5:0
6:0
7:0
8:0
9:0
10:0
11:0
12:0
13:0
14:0
15:0
16:0
17:0
18:0
19:0
20:0

I want to replace the line 3:0 with 3:1
If i use
Code:
sed -i  's/3:0/3:1/' temp.tmp

it will change the line 3:0 with 3:1 and also the line 13:0 with 13:1
1st question is how to change only the first line?
and 2nd question is how to use a variable for the line to change?
for example lets say
Code:
a=3

i cant use
Code:
sed -i 's/$a:0/$a:1/' temp.tmp

how can this bee done?

Thanks in advance for any replie.


Top
 Profile  
 PostPosted: Sun Sep 11, 2011 12:14 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Your first question can be solved by using ^ (begin of line) in your regexp, so that it will match a 3 at the beginning of the line.
Your second question can be solved by using double quotes instead of single quotes around your sed.


Top
 Profile  
 PostPosted: Sun Sep 11, 2011 9:58 am   

Joined: Sat Sep 10, 2011 11:48 pm
Posts: 7
Patsie wrote:
Your first question can be solved by using ^ (begin of line) in your regexp, so that it will match a 3 at the beginning of the line.
Your second question can be solved by using double quotes instead of single quotes around your sed.


Thanks very much for the replie
the double quotes worked fine but can't figure out how to use ^.
can you please write the complete command.
note that i am planning to use this in a larger file, so i will have to distinguish the 25 from 225 as well.


Top
 Profile  
 PostPosted: Tue Sep 20, 2011 11:59 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
nikosliapis wrote:
1st question is how to change only the first line?

Code:
sed 'N; s/3:0/3:1/' temp.tmp

This will change only the first line that occurs.

or
Code:
sed 's/^3:0/3:1/' temp.tmp


The ^ syntax simply tells that the line begins by 3.

nikosliapis wrote:
2nd question is how to use a variable for the line to change?

Code:
a=3 sed "s/$a:0/$a:1/" temp.tmp


And... I would use:
Code:
a=3 sed "/^$a/ {s/0/1/}" temp.tmp


Top
 Profile  
 PostPosted: Thu Sep 22, 2011 11:21 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
jsz wrote:
I would use:
Code:
a=3 sed "/^$a/ {s/0/1/}" temp.tmp

Wouldn't that replace '10:0' with '11:0' and '20:0' with '21:0'?
(with a=10 and a=20 respectively ofcourse...)


Top
 Profile  
 PostPosted: Sun Oct 23, 2011 4:38 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Yes, you're right.
Forget my last solution, nikosliapis.


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: Bing [Bot] and 10 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