Home > Archive > AWK > June 2004 > Re: Delete line above and below the line which matches a pattern -
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 |
Re: Delete line above and below the line which matches a pattern -
|
|
| William Park 2004-06-03, 4:31 pm |
| L Nambadan <lonapan@hotmail.com> wrote:
> Hi,
>
> I want to search for a specific pattern in a file and delete one line
> above and one line below the line which matches the pattern. I'm
> trying to find out how I could do this in awk and could not find any
> suitable examples.
>
> Any pointers will be appreciated.
>
> Thanks !
>
> PS: Is it easier to do this in sed ?
Hint:
- get the line number of all the matching lines, ie. grep -n
- for each n, replace it with 2 numbers, ie. n-1, n+1
- sort | uniq
- delete those lines, ie. sed -e '10d'
--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
No, I will not fix your computer! I can reformat your harddisk, though.
| |
| Charles Demas 2004-06-03, 7:30 pm |
| In article <53777be.0406030934.132bb419@posting.google.com>,
L Nambadan <lonapan@hotmail.com> wrote:
>Hi,
>
>I want to search for a specific pattern in a file and delete one line
>above and one line below the line which matches the pattern. I'm
>trying to find out how I could do this in awk and could not find any
>suitable examples.
>
>Any pointers will be appreciated.
>
>Thanks !
>
>PS: Is it easier to do this in sed ?
it's easiest using Gnu grep.
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| Patrick TJ McPhee 2004-06-04, 3:55 am |
| In article <53777be.0406030934.132bb419@posting.google.com>,
L Nambadan <lonapan@hotmail.com> wrote:
% I want to search for a specific pattern in a file and delete one line
% above and one line below the line which matches the pattern.
Just defer printing until you know you haven't matched the
pattern. If you don't have to worry about lines with two patterns
in a row, it could be
$0 ~ pattern { getline; getline; saveline = $0; next }
NR == 1 { saveline = $0; next }
{ print saveline; saveline = $0 }
END { if (saveline == $0) print }
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
| |
| Alan G Isaac 2004-06-04, 3:55 pm |
|
"Patrick TJ McPhee" <ptjm@interlog.com> wrote in message
news:c9oibs$m5e$4@news.eusc.inter.net...
> $0 ~ pattern { getline; getline; saveline = $0; next }
> NR == 1 { saveline = $0; next }
> { print saveline; saveline = $0 }
> END { if (saveline == $0) print }
I think he wanted to keep the matching line,
so maybe changing your first line to
$0 ~ pattern {print; getline; getline; saveline = $0; next }
will do it.
AlanIsaac
|
|
|
|
|