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 ?
|
|
0
|
|
|
|
Reply
|
lonapan (12)
|
6/3/2004 5:34:48 PM |
|
On 3 Jun 2004 10:34:48 -0700, a posting issued forth from L Nambadan...
> 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 ?
**UNTESTED**
/SomePattern/ {
getline
getline last
next
}
FNR > 1 {
print last
}
{
last = $0
}
END {
print last
}
This assumes that the last line isn't the line following a match. Some
fancier checking will fix this assumption.
HTH,
Jacob
|
|
0
|
|
|
|
Reply
|
Jacob
|
6/3/2004 5:43:44 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.
|
|
0
|
|
|
|
Reply
|
William
|
6/3/2004 7:41:08 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
|
|
0
|
|
|
|
Reply
|
demas
|
6/3/2004 10:35:18 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
ptjm
|
6/4/2004 1:13:32 AM
|
|
"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
|
|
0
|
|
|
|
Reply
|
Alan
|
6/4/2004 2:36:19 PM
|
|
|
5 Replies
425 Views
(page loaded in 0.083 seconds)
Similiar Articles: Delete line above and below the line which matches a pattern ...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 ... remove junk characters - comp.lang.perl.misc... when it is the framing character for the string literal, e.g., 'DELETE ... in TSO under ... unix.solaris... accounts before # permanently removing them with 'remove ... line ... remove any commas/data from mysql? - comp.databases.mysql ...> > How would I change the above to remove any commas ? ... _POST as $key => $value){ if (preg_match($pattern ... Remove extra spaces and line endings - comp.databases.filemaker ... perl + regex bug? - comp.lang.perl.miscIf the above pattern did not match, $1 will be whatever it ... orbitals (for example the 20 lines of numbers including a header line ... 1usa@llenroc.ude.invalid> (remove ... wildcard matching algorithm - comp.lang.rexxI made some changes to the above and it ... graphics.apps.paint-shop-pro ... pattern match and dw in vim - comp.unix.admin Remove ... Introduction The code described below is ... sed: printing only between {} - comp.unix.shell... locale ===== and is released under ... sed, remove last new line - comp.lang.awk I am not ... you to print only specific lines based on the line number or pattern matches ... split at a pattern - comp.lang.awk... the EODs to go into the file too, just remove the ';next' in the above ... gawk --nostalgia awk: bailing out near line ... at a pattern - comp.lang.awk Join two lines in pattern ... Wildcard match? - comp.unix.shell... Stephane, Can you please explain how the above ... differentiate that case, we use another pattern: [*].txt that also matches ... Introduction The code described below is ... sed queation - remove all characters after a hyphen - comp.unix ...... the above solution. > > Note I actually want to remove the hyphen, so the subject line is ... RegExp pattern ... new line - comp.lang.awk How to use sed to delete all lines ... ksh or sed search & replace - comp.unix.programmer... sed -e "s/x/y/g" > oldfile I tried the above. But ... want to only modify the files that contain the pattern ... sed replace string when line match something - comp.unix ... Delete line above and below the line which matches a pattern ...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 ... using sed - how do I delete lines above and below a match?: delete ...using sed - I need to delete one line above and one line below any line ... how, do, I, delete, lines, above, and, below, a, match? sed; non-matching pattern through ... 7/24/2012 10:05:06 AM
|