Delete line above and below the line which matches a pattern -

  • Follow


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:













7/24/2012 10:05:06 AM


Reply: