How to replace a whole line (matching a pattern) in a text filke with SED ?

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Matt Bens » Sat, 30 Aug 2003 00:48:20



Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
among other lines containing the text fragment pattern "version" (without quotes).
Other characters can appear in these lines as well.

Now I want to replace ALL lines in this file with the text line "vers. release"
(without quotes). All other lines should remain untouched.
Order of lines should remain as well.

Example blah.dat:

kikik ikik ik kik kik  
hello version endofline
rtrtrtrtrtrtrt trtrtr

afterwards:

kikik ikik ik kik kik  
vers. release
rtrtrtrtrtrtrt trtrtr

I guess that this can be done most comfortable with the sed.
How do I do that ?

Thx
Matt

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Charles Dem » Sat, 30 Aug 2003 00:53:49




>Assume I have a text file blah.dat. In that file there can be a line (or
>multiple lines)
>among other lines containing the text fragment pattern "version"
>(without quotes).
>Other characters can appear in these lines as well.

>Now I want to replace ALL lines in this file with the text line "vers. release"
>(without quotes). All other lines should remain untouched.
>Order of lines should remain as well.

>Example blah.dat:

>kikik ikik ik kik kik  
>hello version endofline
>rtrtrtrtrtrtrt trtrtr

>afterwards:

>kikik ikik ik kik kik  
>vers. release
>rtrtrtrtrtrtrt trtrtr

>I guess that this can be done most comfortable with the sed.
>How do I do that ?

one way:

sed 's/.*version.*/vers. release/' infile > outfile

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.


 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Ed Morto » Sat, 30 Aug 2003 00:59:13



Quote:> Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
> among other lines containing the text fragment pattern "version" (without quotes).
> Other characters can appear in these lines as well.

> Now I want to replace ALL lines in this file with the text line "vers. release"
> (without quotes). All other lines should remain untouched.
> Order of lines should remain as well.

> Example blah.dat:

> kikik ikik ik kik kik  
> hello version endofline
> rtrtrtrtrtrtrt trtrtr

> afterwards:

> kikik ikik ik kik kik  
> vers. release
> rtrtrtrtrtrtrt trtrtr

> I guess that this can be done most comfortable with the sed.
> How do I do that ?

sed '/version/s/^.*$/vers.release/'

        Ed.

- Show quoted text -

Quote:> Thx
> Matt

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Robert Kat » Sat, 30 Aug 2003 01:42:36




>>Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
>>among other lines containing the text fragment pattern "version" (without quotes).
>>Other characters can appear in these lines as well.

>>Now I want to replace ALL lines in this file with the text line "vers. release"
>>(without quotes). All other lines should remain untouched.
>>Order of lines should remain as well.

>>Example blah.dat:

>>kikik ikik ik kik kik  
>>hello version endofline
>>rtrtrtrtrtrtrt trtrtr

>>afterwards:

>>kikik ikik ik kik kik  
>>vers. release
>>rtrtrtrtrtrtrt trtrtr

>>I guess that this can be done most comfortable with the sed.
>>How do I do that ?

> sed '/version/s/^.*$/vers.release/'

sed '/version/s/.*/vers.release/'

--
Regards,

---Robert

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Davide Bianch » Sat, 30 Aug 2003 01:54:35



Quote:> among other lines containing the text fragment pattern "version"
> (without quotes).
> Now I want to replace ALL lines in this file with the text line
> "vers. release"

very fast and without checking..

sed 's/^.*version.*$/vers. release/' blah.dat > blah.dat.new

man sed for more info

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Barry Margoli » Sat, 30 Aug 2003 02:50:49




>Assume I have a text file blah.dat. In that file there can be a line (or
>multiple lines)
>among other lines containing the text fragment pattern "version" (without
>quotes).
>Other characters can appear in these lines as well.

>Now I want to replace ALL lines in this file with the text line "vers. release"
>(without quotes). All other lines should remain untouched.

sed '/version/s/^.*$/vers. release/'

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Ed Murph » Sat, 30 Aug 2003 03:31:36



> Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
> among other lines containing the text fragment pattern "version" (without quotes).
> Other characters can appear in these lines as well.

> Now I want to replace ALL lines in this file with the text line "vers. release"
> (without quotes). All other lines should remain untouched.
> Order of lines should remain as well.
[snip]
> I guess that this can be done most comfortable with the sed.
> How do I do that ?

cat blah.dat | sed -e 's/^.*version.*$/vers. release/' > new_blah.dat
 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by laura fairhe » Sat, 30 Aug 2003 03:46:55



>Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
>among other lines containing the text fragment pattern "version" (without quotes).
>Other characters can appear in these lines as well.

>Now I want to replace ALL lines in this file with the text line "vers. release"
>(without quotes). All other lines should remain untouched.
>Order of lines should remain as well.

>Example blah.dat:

>kikik ikik ik kik kik  
>hello version endofline
>rtrtrtrtrtrtrt trtrtr

>afterwards:

>kikik ikik ik kik kik  
>vers. release
>rtrtrtrtrtrtrt trtrtr

>I guess that this can be done most comfortable with the sed.
>How do I do that ?

You could use the 'c' command,

/version/c\
vers. release

Quote:

>Thx
>Matt

btw: The original and invaluable manual for 'sed' is available on my web-site

     http://lf.1accesshost.com

byefornow
l

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by peter pils » Sat, 30 Aug 2003 03:51:26



> I guess that this can be done most comfortable with the sed.
> How do I do that ?

dont  use sed, but if you have perl installed (you most propably have),
then you could do just:

cat x.dat | perl -e 'while (<STDIN>) {s/^.*version.*/vers.release/;print}'

hope that help,
peter

--
peter pilsl

http://www.goldfisch.at

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Charles Dem » Sat, 30 Aug 2003 04:06:46





>> I guess that this can be done most comfortable with the sed.
>> How do I do that ?

>dont  use sed, but if you have perl installed (you most propably have),
>then you could do just:

>cat x.dat | perl -e 'while (<STDIN>) {s/^.*version.*/vers.release/;print}'

and what would the advantage of this as opposed to a sed solution:

sed 's/.*version.*/vers. release/' x.dat

or

sed '/version/s/.*/vers. release/' x.dat

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Bill Marcu » Sat, 30 Aug 2003 17:56:30


On Thu, 28 Aug 2003 17:48:20 +0200, Matt Benson

> Assume I have a text file blah.dat. In that file there can be a line
> (or multiple lines) among other lines containing the text fragment
> pattern "version" (without quotes). Other characters can appear in
> these lines as well.

> Now I want to replace ALL lines in this file with the text line "vers.
> release" (without quotes). All other lines should remain untouched.
> Order of lines should remain as well.

> Example blah.dat:

> kikik ikik ik kik kik  
> hello version endofline
> rtrtrtrtrtrtrt trtrtr

> afterwards:

> kikik ikik ik kik kik  
> vers. release
> rtrtrtrtrtrtrt trtrtr

> I guess that this can be done most comfortable with the sed.
> How do I do that ?

> Thx
> Matt

sed 's/.*version.*/vers. release/' oldfile > newfile

--
Commander Spiral Pyjama Pseudo-Rhinocerous Feline Thingamajig Bill Marcum
(the First)
Ozy and Millie Name Generator http://heifong.phase.org/omname.php

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Robert Kat » Sat, 30 Aug 2003 22:53:14





>>Assume I have a text file blah.dat. In that file there can be a line (or
>>multiple lines)
>>among other lines containing the text fragment pattern "version" (without
>>quotes).
>>Other characters can appear in these lines as well.

>>Now I want to replace ALL lines in this file with the text line "vers. release"
>>(without quotes). All other lines should remain untouched.

> sed '/version/s/^.*$/vers. release/'

     sed '/version/s/.*/vers.release/'

--
Regards,

---Robert

 
 
 

How to replace a whole line (matching a pattern) in a text filke with SED ?

Post by Juergen Hec » Sun, 31 Aug 2003 00:33:27


Matt Benson wroteb:

Quote:

> Assume I have a text file blah.dat. In that file there can be a line (or multiple lines)
> among other lines containing the text fragment pattern "version" (without quotes).
> Other characters can appear in these lines as well.

> Now I want to replace ALL lines in this file with the text line "vers. release"
> (without quotes). All other lines should remain untouched.
> Order of lines should remain as well.

> Example blah.dat:

> kikik ikik ik kik kik
> hello version endofline
> rtrtrtrtrtrtrt trtrtr

> afterwards:

> kikik ikik ik kik kik
> vers. release
> rtrtrtrtrtrtrt trtrtr

> I guess that this can be done most comfortable with the sed.
> How do I do that ?

> Thx
> Matt

If you not insist in sed, you may use vi in scripting mode, i.e.

vi -c '%s/.*version.*/vers. release/ | wq' blah.dat > /dev/null

Regards
Juergen