Delete a whole line with sed, then insert diff text on same line...

Delete a whole line with sed, then insert diff text on same line...

Post by mat » Thu, 22 May 2003 07:06:51



Ok, I've searched through this group about 'deleting' and 'replacing'
with sed and still couldnt find the answer to my question.

Basically, i want to search for a certain string in a file, then
delete the entire line.  Immediately after this, i would like to
insert another string on the same line.  Is this possible?

So, lets say that in every file I need to search, there is one line of
code that starts with "My Text:" but the text following this string
(on the same line) is different in every file.  I would like to delete
this line entirely and insert new, different text here instead, such
as "Your Text..."

Let me know if this doesnt make sense, and i appreciate any help that
is given.  Thanks...

Matt

 
 
 

Delete a whole line with sed, then insert diff text on same line...

Post by Chuck Geigne » Thu, 22 May 2003 07:16:40



> Ok, I've searched through this group about 'deleting' and 'replacing'
> with sed and still couldnt find the answer to my question.

> Basically, i want to search for a certain string in a file, then
> delete the entire line.  Immediately after this, i would like to
> insert another string on the same line.  Is this possible?

> So, lets say that in every file I need to search, there is one line of
> code that starts with "My Text:" but the text following this string
> (on the same line) is different in every file.  I would like to delete
> this line entirely and insert new, different text here instead, such
> as "Your Text..."

Try this: (do a dry run to see before replacing "filename" with "filename.1"
sed 's/^My Text:.*/Your Text/g' filename > filename.1

HTH
--
Chuck Geigner --------------------------------------------------------
AIX SysOp,
Milner Library, Illinois State Univ.
"Been borrowing Occam's Razor since 1992 - Haven't cut myself yet."
http://www.chux0r.org ______________________________________ WYGIWISYG

 
 
 

Delete a whole line with sed, then insert diff text on same line...

Post by Charles Dem » Fri, 23 May 2003 05:04:02





>> Ok, I've searched through this group about 'deleting' and 'replacing'
>> with sed and still couldnt find the answer to my question.

>> Basically, i want to search for a certain string in a file, then
>> delete the entire line.  Immediately after this, i would like to
>> insert another string on the same line.  Is this possible?

>> So, lets say that in every file I need to search, there is one line of
>> code that starts with "My Text:" but the text following this string
>> (on the same line) is different in every file.  I would like to delete
>> this line entirely and insert new, different text here instead, such
>> as "Your Text..."

>Try this: (do a dry run to see before replacing "filename" with "filename.1"
>sed 's/^My Text:.*/Your Text/g' filename > filename.1

sed 's/^My Text:.*/Your Text/' filename > filename.1

the g serves no function when you use the ^, only one replacement is
possible.

Another way is:

sed '/^My Text:/ s/.*/Your Text/' filename > filename.1

Chuck Demas

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

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

 
 
 

Delete a whole line with sed, then insert diff text on same line...

Post by rakesh shar » Fri, 23 May 2003 14:23:45



> Ok, I've searched through this group about 'deleting' and 'replacing'
> with sed and still couldnt find the answer to my question.

                 when in doubt,
                      man sed

Quote:> So, lets say that in every file I need to search, there is one line of
> code that starts with "My Text:" but the text following this string
> (on the same line) is different in every file.  I would like to delete
> this line entirely and insert new, different text here instead, such
> as "Your Text..."

sed -e '/^My text:/c\
Your Text...'  inputfile
 
 
 

Delete a whole line with sed, then insert diff text on same line...

Post by Chuck Geigne » Sat, 24 May 2003 01:18:05






<snip>
>>>So, lets say that in every file I need to search, there is one line of
>>>code that starts with "My Text:" but the text following this string
>>>(on the same line) is different in every file.  I would like to delete
>>>this line entirely and insert new, different text here instead, such
>>>as "Your Text..."

>>Try this: (do a dry run to see before replacing "filename" with "filename.1"
>>sed 's/^My Text:.*/Your Text/g' filename > filename.1

> sed 's/^My Text:.*/Your Text/' filename > filename.1

> the g serves no function when you use the ^, only one replacement is
> possible.

Oops - you're right! Glad someone's checking my work =) Cheers Chuck!

--
Chuck Geigner --------------------------------------------------------
AIX SysOp,
Milner Library, Illinois State Univ.
"Been borrowing Occam's Razor since 1992 - Haven't cut myself yet."
http://www.chux0r.org ______________________________________ WYGIWISYG

 
 
 

1. Find a string, delete that line, delete 1 line before it and all lines after it...

Ok, so in my code example below, i would like to first search for the
line containing the "-- CHANGE HISTORY" string.  Then, I would like
delete the ONE line before it (i.e. the "-- ------etc..." line).
Then, i would like to delete EVERY line to the end of the file after
the CHANGE HISTORY string line.  Finally, i would like to delete line
containing the CHANGE HISTORY string.

So far, i'm able to delete all the lines after CHANGE HISTORY by using

sed -e '/-- CHANGE HISTORY:/q' myfile.ada

but this leaves the
-- ----------------------------------------                          
-- CHANGE HISTORY:  

remaining.  Any help will be appreciated, and i'd like to use SED if
possible (rather than awk, ksh scripts, etc.)

Thanks...

-- ----------------------------------------
-- CHANGE HISTORY:
-- All remaining text after the "-- CHANGE HISTORY" string should be
deleted
-- as well....
etc...
etc...
....
....
<<End of File>>

2. Eudora -> Linux Pop3 server Very Slow

3. Insert a line of text every nth line

4. ntop RedHat 7.1 not running! Help

5. inserting a blank line after every line - using sed

6. pppd and routing

7. inserting lines before and after lines with matching pattern using sed?

8. Please Help with KDE crash only as root (KFM part of problem?)

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

10. Inserting a new line of text using SED

11. Just wondering: sed could not insert or append text in the current line?

12. Sed command, Insert text in front of existing line

13. sed: how to insert text lines