LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2002, 03:37 PM   #1
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Rep: Reputation: 15
Sed help


Anyone know the comand line to replace a string after a word for the 1st line?

i want to replace the number 266 with 100 for the first occurance after the date YYMMDD on the first line.

This is what i have so far

cDATE=`date '+%y%m%d'`

sed '/'${cDATE}'/,$ { 1s/266/100 }' <file> > <newfile>


however it keeps giving me garbled sed error and i can't for the life of me figure out what's wrong.
 
Old 04-18-2002, 01:15 PM   #2
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Original Poster
Rep: Reputation: 15
bump
 
Old 04-19-2002, 08:04 AM   #3
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
I'm not an sed expert so I had to do some reading first. I'm not sure exactly what you want but I think this gets pretty close:

cDATE=`date '+%y%m%d'`
sed -e "/^$cDATE/,/^266/s/266/100/" file > file.new

This would substitute every occurance of 266 with 100 in a block starting with $cDATE and ending with 266.
But you said something about the first line. Maybe something like this would be more what you want:

sed -e "1s/\($cDATE\)\(.*\)266/\1\2100/" file > file.new

This would look for the pattern date-anything-266 and substitute it with firstmatchedpart-secondmatchedpart-100. Because of the 1 in the beginning it would only do it on the first line.

Oh yeah now I remember why that last one won't work. Sed matches the longest match so if you have 266 twice on that line only the last one will be replaced.

Last edited by Mik; 04-19-2002 at 08:18 AM.
 
Old 04-19-2002, 10:43 AM   #4
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks, that's exactly what i wanted. To match the first occurance on the first line after the 'date'.

Moocha gracia!

Noodle.
 
Old 04-19-2002, 11:37 AM   #5
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Original Poster
Rep: Reputation: 15
One last question, what if there's 2 occurances on that line? How would you modify u're 2nd example to change only the 1st occurnace?
 
Old 04-22-2002, 05:58 AM   #6
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
I tried to figure that one out but couldn't really find a solution to that. The second example you can't modify to change only the first one. Because that's just the way sed works.
I managed to get the first example working only for the first line using a script file for the commands but then it won't substitute the $cDATE variable anymore. I'd have to read up on sed a bit more to figure that one out.
 
Old 04-22-2002, 10:08 AM   #7
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks, u're example is working great for my script now, i just hope that there's just going to be one occurance on those first lines.

thanks.
 
  


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
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Sed q? ky-lab_rat Linux - Software 5 10-26-2004 08:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 11:52 AM
help with sed DavidPhillips Programming 3 08-11-2003 05:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

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