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

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

Post by mat » Sat, 31 May 2003 06:57:15



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>>

 
 
 

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

Post by Stephane CHAZELA » Sat, 31 May 2003 14:50:46



> 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.

[...]

sed -ne '/^-- CHANGE HISTORY:$/q' -e 'x;1d;p'

--
Stphane

 
 
 

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

Post by rakesh shar » Sat, 31 May 2003 17:03:35


starting from where u left off: (substitute u'r string -- CHANGE HISTORY:
whenever u see regex)

  sed -e '/regex/q' inputfile | sed -e 'N;$!P;$!D;$d'
  ^^^^^^^^^^^^^^^^^^^^^^               ^^^^^^^^^^^^^^
   get lines upto CHANGE HIST..        delete last 2 lines

or u can do it all with 1 call to sed:

sed -e '
:loop
$!N
/\nregex$/!bloop
s///
s/^\(.*\)\n.*$/\1/
q
' inputfile

or:
sed -ne '/regex/!{H;b;};x;s/^\n//;s/^\(.*\)\n.*$/\1/p;q' inputfile

Note: this solution assumes a particular format of your file, viz.,
 a) line containing regex CANNOT be the last line of the file.
 b) file length MUST be 3 lines or more.
 c) file MUST contain the regex.

Quote:> 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>>

 
 
 

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

Post by Michael Wa » Sat, 31 May 2003 21:07:23





>> 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.
>[...]

>sed -ne '/^-- CHANGE HISTORY:$/q' -e 'x;1d;p'

why not

sed -ne '/^-- CHANGE HISTORY:$/q;x;1d;p'

?
--

 
 
 

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

Post by Stephane CHAZELA » Sat, 31 May 2003 21:29:12


[...]

Quote:>>sed -ne '/^-- CHANGE HISTORY:$/q' -e 'x;1d;p'

> why not

> sed -ne '/^-- CHANGE HISTORY:$/q;x;1d;p'

No special reason. I first started with

sed -e '/^-- CHANGE HISTORY:$/{d;q;}' -e 'x;1d'
(here the second -e is necessary)

Then realized it was simpler with -n and forgot to join the two
expressions.

--
Stphane

 
 
 

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

Post by John W. Krah » Sun, 01 Jun 2003 11:08:16



> 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.)


John
--
use Perl;
program
fulfillment

 
 
 

1. Delete remaining lines below a found string?

Hi,

What I want to do is this: in a largish file, find a particular string,
and then delete the remaining lines below that string.

Does anyone know any way to do this using a combination of a shell script
and either awk or sed? I can't seem to find a way to do this.

Thanks in advance,
Matt

--

| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
m-a-t-t-h-e-w r-o-s-s d-a-v-i-s                university of maryland
http://www.butterfly.net/mozart                       school of music
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

2. Error compiling linux-2.1.16

3. Help in finding a string and deleting all the lines containing it

4. samba for aix 4.3.3

5. find a string in a file then delete the next n lines

6. Linux 1.0 corrupted my OS/2 partition table

7. Deleting multplie lines after matching a regex in the first line

8. Error with 3c509 and SOICSIFADDR

9. . sed script: keep 1 line, delete next 7 lines

10. Extracting multiple lines OR deleting multiple lines from a file using AWK

11. Delete the first line of a 1.4 million lines file

12. Deleting lines above a given line

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