* [gentoo-user] Grep question
@ 2009-03-02 2:01 Adam Carter
2009-03-02 2:04 ` Mike Kazantsev
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Adam Carter @ 2009-03-02 2:01 UTC (permalink / raw
To: gentoo-user@lists.gentoo.org
[-- Attachment #1: Type: text/plain, Size: 244 bytes --]
I need to select all the lines between string1 and string2 in a file. String1 exists on an entire line by itself and string2 will be at the start of a line. What's the syntax? I cant use -A as there is a variable number of lines.
Thanks.
[-- Attachment #2: Type: text/html, Size: 781 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [gentoo-user] Grep question
2009-03-02 2:01 [gentoo-user] Grep question Adam Carter
@ 2009-03-02 2:04 ` Mike Kazantsev
2009-03-02 10:01 ` Steven Lembark
2009-03-02 14:06 ` [gentoo-user] " James
2 siblings, 0 replies; 9+ messages in thread
From: Mike Kazantsev @ 2009-03-02 2:04 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 619 bytes --]
On Mon, 2 Mar 2009 13:01:31 +1100
Adam Carter <Adam.Carter@optus.com.au> wrote:
> I need to select all the lines between string1 and string2 in a file. String1 exists on an entire line by itself and string2 will be at the start of a line. What's the syntax? I cant use -A as there is a variable number of lines.
I doubt there's a solution involving grep, unless you use it twice in
the same pipe:
grep -A9999 string1 /some/file | grep -B 9999 string2
But there can be any amount of more elegant solutions, involving
sed:
sed -n '/string1/,/string2/p' /some/file
--
Mike Kazantsev // fraggod.net
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [gentoo-user] Grep question
2009-03-02 2:01 [gentoo-user] Grep question Adam Carter
2009-03-02 2:04 ` Mike Kazantsev
@ 2009-03-02 10:01 ` Steven Lembark
2009-03-02 14:06 ` [gentoo-user] " James
2 siblings, 0 replies; 9+ messages in thread
From: Steven Lembark @ 2009-03-02 10:01 UTC (permalink / raw
To: gentoo-user
Adam Carter wrote:
> I need to select all the lines between string1 and string2 in a file.
> String1 exists on an entire line by itself and string2 will be at the
> start of a line. What's the syntax? I cant use -A as there is a variable
> number of lines.
Perl will handle this easily enough for you.
Assuming you want to print string1 and string2:
perl -n -e 'print if /string1/ ../string2/';
The '..' notation behaves sort of like a triac
(flip-flop?): it is false until the first test
is true and true until the second passes, at
which point it stays false again.
for example:
$ cat a
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
foo <-- /foo/ true here
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
bar <-- /bar/ true here
fdsa
fdsa
fdsa
$ perl -n -e 'print if /foo/ .. /bar/';
foo
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
bar
--
Steven Lembark 85-09 90th St.
Workhorse Computing Woodhaven, NY, 11421
lembark@wrkhors.com +1 888 359 3508
^ permalink raw reply [flat|nested] 9+ messages in thread
* [gentoo-user] Re: Grep question
2009-03-02 2:01 [gentoo-user] Grep question Adam Carter
2009-03-02 2:04 ` Mike Kazantsev
2009-03-02 10:01 ` Steven Lembark
@ 2009-03-02 14:06 ` James
2009-03-03 2:10 ` Harry Putnam
2 siblings, 1 reply; 9+ messages in thread
From: James @ 2009-03-02 14:06 UTC (permalink / raw
To: gentoo-user
Adam Carter <Adam.Carter <at> optus.com.au> writes:
> I need to select all
> the lines between string1 and string2 in a file. String1 exists on
an entire
> line by itself and string2 will be at the start of a line. What's
the syntax? I
> cant use -A as there is a variable number of lines.
AWK
is my vote. Old, *SIMPLE* and used by most other packages when pattern matching
is involved. Often AWK and SED go together..... As do Perl and AWK
http://www.softpanorama.org/Tools/awk.shtml
hth,
James
^ permalink raw reply [flat|nested] 9+ messages in thread
* [gentoo-user] Re: Grep question
2009-03-02 14:06 ` [gentoo-user] " James
@ 2009-03-03 2:10 ` Harry Putnam
2009-03-03 9:52 ` Etaoin Shrdlu
0 siblings, 1 reply; 9+ messages in thread
From: Harry Putnam @ 2009-03-03 2:10 UTC (permalink / raw
To: gentoo-user
James <wireless@tampabay.rr.com> writes:
> Adam Carter <Adam.Carter <at> optus.com.au> writes:
>
>
>> I need to select all
>> the lines between string1 and string2 in a file. String1 exists on
> an entire
>> line by itself and string2 will be at the start of a line. What's
> the syntax? I
>> cant use -A as there is a variable number of lines.
>
> AWK
>
> is my vote. Old, *SIMPLE* and used by most other packages when
> pattern matching is involved. Often AWK and SED go together..... As
> do Perl and AWK
Yup and using Steves' example:
$ cat a
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
foo <-- /foo/ true here
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
bar <-- /bar/ true here
fdsa
fdsa
fdsa
cat a | awk '/^foo/{FLAG=1}\
FLAG{print} \
/^bar/{FLAG=""}'
foo
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
bar
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [gentoo-user] Re: Grep question
2009-03-03 2:10 ` Harry Putnam
@ 2009-03-03 9:52 ` Etaoin Shrdlu
2009-03-05 16:54 ` Harry Putnam
0 siblings, 1 reply; 9+ messages in thread
From: Etaoin Shrdlu @ 2009-03-03 9:52 UTC (permalink / raw
To: gentoo-user
On Tuesday 3 March 2009, 03:10, Harry Putnam wrote:
> cat a | awk '/^foo/{FLAG=1}\
> FLAG{print} \
> /^bar/{FLAG=""}'
awk '/^foo/,/^bar/' a
does the same :)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [gentoo-user] Re: Grep question
2009-03-03 9:52 ` Etaoin Shrdlu
@ 2009-03-05 16:54 ` Harry Putnam
2009-03-05 23:01 ` Adam Carter
0 siblings, 1 reply; 9+ messages in thread
From: Harry Putnam @ 2009-03-05 16:54 UTC (permalink / raw
To: gentoo-user
Etaoin Shrdlu <shrdlu@unlimitedmail.org> writes:
> On Tuesday 3 March 2009, 03:10, Harry Putnam wrote:
>
>> cat a | awk '/^foo/{FLAG=1}\
>> FLAG{print} \
>> /^bar/{FLAG=""}'
>
> awk '/^foo/,/^bar/' a
>
> does the same :)
Nice...
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [gentoo-user] Re: Grep question
2009-03-05 16:54 ` Harry Putnam
@ 2009-03-05 23:01 ` Adam Carter
2009-03-06 9:33 ` Etaoin Shrdlu
0 siblings, 1 reply; 9+ messages in thread
From: Adam Carter @ 2009-03-05 23:01 UTC (permalink / raw
To: gentoo-user@lists.gentoo.org
> > awk '/^foo/,/^bar/' a
> >
> > does the same :)
>
> Nice...
Thanks for all these answers. Interesingly when I moved the sed script (sed "s/;/\\n/g") from Linux to Solaris it failed as Solaris sed doesn't like putting the newline character as the "translated to" bit. Installing GNU sed on the Solaris box sorted that out.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [gentoo-user] Re: Grep question
2009-03-05 23:01 ` Adam Carter
@ 2009-03-06 9:33 ` Etaoin Shrdlu
0 siblings, 0 replies; 9+ messages in thread
From: Etaoin Shrdlu @ 2009-03-06 9:33 UTC (permalink / raw
To: gentoo-user
On Friday 6 March 2009, 00:01, Adam Carter wrote:
> > > awk '/^foo/,/^bar/' a
> > >
> > > does the same :)
> >
> > Nice...
>
> Thanks for all these answers. Interesingly when I moved the sed script
> (sed "s/;/\\n/g") from Linux to Solaris it failed as Solaris sed
> doesn't like putting the newline character as the "translated to" bit.
> Installing GNU sed on the Solaris box sorted that out.
Or for the future, with seds that do not like \n in the rhs, you can do
sed 's/;/\
/g'
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2009-03-06 9:36 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-02 2:01 [gentoo-user] Grep question Adam Carter
2009-03-02 2:04 ` Mike Kazantsev
2009-03-02 10:01 ` Steven Lembark
2009-03-02 14:06 ` [gentoo-user] " James
2009-03-03 2:10 ` Harry Putnam
2009-03-03 9:52 ` Etaoin Shrdlu
2009-03-05 16:54 ` Harry Putnam
2009-03-05 23:01 ` Adam Carter
2009-03-06 9:33 ` Etaoin Shrdlu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox