|
Home > Archive > Unix Shell > November 2006 > remove several lines from a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
remove several lines from a file
|
|
| osiris@abydos.kmt 2006-11-13, 9:01 am |
| Is there a way to remove several lines from a file
(using e.g. sed or perl) where the lines have a pattern
beginning with a line-feed and pattern and
ending with a line-feed and blank-line?
It would be the the exact opposite
of the awk command:
awk '/^firstpattern/,/^$/' myfile
Many thanks.
| |
| Janis Papanagnou 2006-11-13, 9:01 am |
| osiris@abydos.kmt wrote:
> Is there a way to remove several lines from a file
> (using e.g. sed or perl) where the lines have a pattern
> beginning with a line-feed and pattern and
> ending with a line-feed and blank-line?
>
> It would be the the exact opposite
> of the awk command:
>
> awk '/^firstpattern/,/^$/' myfile
"The exact opposite"?
awk '/^firstpattern/,/^$/ {next} 1' myfile
Janis
>
> Many thanks.
| |
| osiris@abydos.kmt 2006-11-13, 9:01 am |
|
On Sun, 12 Nov 2006 02:36:40 +0100, Janis Papanagnou <Janis_Papanagnou@hotmail.com> chiseled on limestone:
>
>osiris@abydos.kmt wrote:
>
>"The exact opposite"?
Thanks for your help and sorry for my lack of clarification.
By opposite, I meant everything else besides the pattern
found by awk above as in 'grep -v mypattern' being the opposite of
'grep mypattern'. Hope that's more clear.
> awk '/^firstpattern/,/^$/ {next} 1' myfile
This does do it correctly using nawk on my system
however, I realized that I can't really have a temporary file
so I have to use /bin/ed or perl. Can /bin/ed or PERL do this?
| |
| Jon LaBadie 2006-11-13, 9:01 am |
| osiris@abydos.kmt wrote:
> On Sun, 12 Nov 2006 02:36:40 +0100, Janis Papanagnou <Janis_Papanagnou@hotmail.com> chiseled on limestone:
>
> Thanks for your help and sorry for my lack of clarification.
> By opposite, I meant everything else besides the pattern
> found by awk above as in 'grep -v mypattern' being the opposite of
> 'grep mypattern'. Hope that's more clear.
>
>
> This does do it correctly using nawk on my system
> however, I realized that I can't really have a temporary file
> so I have to use /bin/ed or perl. Can /bin/ed or PERL do this?
echo '/^firstpattern/,/^$/d
w
q' | /bin/ed - myfile
| |
| John W. Krahn 2006-11-13, 9:01 am |
| osiris@abydos.kmt wrote:
> Is there a way to remove several lines from a file
> (using e.g. sed or perl) where the lines have a pattern
> beginning with a line-feed and pattern and
> ending with a line-feed and blank-line?
>
> It would be the the exact opposite
> of the awk command:
>
> awk '/^firstpattern/,/^$/' myfile
perl -ne'/^firstpattern/ .. /^$/ or print' myfile
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Janis Papanagnou 2006-11-13, 9:01 am |
| osiris@abydos.kmt wrote:
> On Sun, 12 Nov 2006 02:36:40 +0100, Janis Papanagnou <Janis_Papanagnou@hotmail.com> chiseled on limestone:
>
> Thanks for your help and sorry for my lack of clarification.
> By opposite, I meant everything else besides the pattern
> found by awk above as in 'grep -v mypattern' being the opposite of
> 'grep mypattern'. Hope that's more clear.
Your mentioning of the "line feeds" made me uncertain what you meant.
>
>
>
> This does do it correctly using nawk on my system
> however, I realized that I can't really have a temporary file
> so I have to use /bin/ed or perl. Can /bin/ed or PERL do this?
What do you mean by temporary file? I don't see any temporary file
here.
If your data is not in a file 'myfile' but created by some process
then you'd use awk to read from stdin...
yourprocess | awk '/^firstpattern/,/^$/ {next} 1'
Sed or PERL would not work differently in this respect.
If you mean not to create a separate file with the results then you
should be aware that either your tools or your shell must have to
write a second file before the data source file is erased.
Janis
| |
| osiris@abydos.kmt 2006-11-13, 9:01 am |
|
On Sun, 12 Nov 2006 01:23:16 -0500, Jon LaBadie <jxlabadie@axcxmx.org> carved
in sandstone:
>
>osiris@abydos.kmt wrote:
>l.com> chiseled on limestone:
>
>echo '/^firstpattern/,/^$/d
>w
>q' | /bin/ed - myfile
Thanks for suggesting this, Jon, but unfortunely, it didn't work for me.
I also tried the following (a very similar suggestion to yours)
from a concurrent thread:
I created a 'test' data file:
1111111111111111
2222222222222222
3333333333333333
4444444444444444
5555555555555555
6666666666666666
7777777777777777
8888888888888888
9999999999999999
I then wrote the following one-line script and executed it:
#! /bin/sh -fx
# myscript
( echo '/^4444/,/^$/d' ; echo wq ) | /bin/ed -- $HOME/test
../myscript
The -x output was:
+ echo /^4444/,/^$/d
+ echo wq
+ /bin/ed -- /home/osiris/test
155
?
?
The data file remained unchanged.
What am I doing wrong?
| |
| Jon LaBadie 2006-11-13, 1:17 pm |
| osiris@abydos.kmt wrote:
> On Sun, 12 Nov 2006 01:23:16 -0500, Jon LaBadie <jxlabadie@axcxmx.org> carved
> in sandstone:
>
> Thanks for suggesting this, Jon, but unfortunely, it didn't work for me.
> I also tried the following (a very similar suggestion to yours)
> from a concurrent thread:
>
> I created a 'test' data file:
>
> 1111111111111111
> 2222222222222222
> 3333333333333333
>
> 4444444444444444
> 5555555555555555
> 6666666666666666
>
> 7777777777777777
> 8888888888888888
> 9999999999999999
>
> I then wrote the following one-line script and executed it:
>
> #! /bin/sh -fx
> # myscript
> ( echo '/^4444/,/^$/d' ; echo wq ) | /bin/ed -- $HOME/test
>
> ./myscript
>
> The -x output was:
> + echo /^4444/,/^$/d
> + echo wq
> + /bin/ed -- /home/osiris/test
> 155
> ?
> ?
>
> The data file remained unchanged.
>
> What am I doing wrong?
My bad, I didn't think about multiple blank lines.
Change the "," to a ";"
/^pattern/;/^$/d
|
|
|
|
|