Unix Shell - Bash: Find string plus 2 lines down from string

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2007 > Bash: Find string plus 2 lines down from string





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 Bash: Find string plus 2 lines down from string
ChrisS

2007-08-24, 1:24 pm

Hey all,

I recently came across a command argument (switch) that allowed for
discovering (dear I use the word find) a string in a text file, then
grab a string that's 4 lines down from the original string.

I sounds like it would be grep or egrep, but I can't remember.

Does this make "any" sense to anyone?

I'll check "cut" and "head" maybe.

thanks.

chris

Ed Morton

2007-08-24, 1:24 pm

ChrisS wrote:
> Hey all,
>
> I recently came across a command argument (switch) that allowed for
> discovering (dear I use the word find) a string in a text file, then
> grab a string that's 4 lines down from the original string.
>
> I sounds like it would be grep or egrep, but I can't remember.
>
> Does this make "any" sense to anyone?
>
> I'll check "cut" and "head" maybe.
>
> thanks.
>
> chris
>


awk 'c&&!--c;/string/{c=4}' file

Below is some general ways to perform this type of operation.

Ed.

a) Print all records from some pattern:

awk '/pattern/{f=1}f' file

b) Print all records after some pattern:

awk 'f;/pattern/{f=1}' file

c) Print the Nth record after some pattern:

awk 'c&&!--c;/pattern/{c=N}' file

d) Print every record except the Nth record after some pattern:

awk 'c&&!--c{next}/pattern/{c=N}' file

e) Print the N records after some pattern:

awk 'c&&c--;/pattern/{c=N}' file

f) Print every record except the N records after some pattern:

awk 'c&&c--{next}/pattern/{c=N}' file

I changed the variable name from "f" for "found" to "c" for "count"
where appropriate as that's more expressive of what the variable
actually IS.


Bill Marcum

2007-08-24, 1:24 pm

On Fri, 24 Aug 2007 15:09:33 -0000, ChrisS
<chris.scarff@gmail.com> wrote:
>
>
> Hey all,
>
> I recently came across a command argument (switch) that allowed for
> discovering (dear I use the word find) a string in a text file, then
> grab a string that's 4 lines down from the original string.
>
> I sounds like it would be grep or egrep, but I can't remember.
>
> Does this make "any" sense to anyone?
>

Perhaps it was "grep -A 4", which prints the line containing the string
and the next four lines?

--
It is sweet to let the mind unbend on occasion.
-- Quintus Horatius Flaccus (Horace)
ChrisS

2007-08-24, 1:24 pm

Thanks Bill.

I think that's what I was looking for. I didn't see it in the Sun
provided grep. I'll load GNU grep via the Blastwave packages. That
should take care of that.

Thanks all for the suggestions.

Great Group.

Chris

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com