* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 14:59 I want to list the NEXT line after a string appears in a list of files zzapper
@ 2006-09-01 15:15 ` John Eikenberry
2006-09-02 13:59 ` zzapper
2006-09-01 15:15 ` Frank Terbeck
` (3 subsequent siblings)
4 siblings, 1 reply; 9+ messages in thread
From: John Eikenberry @ 2006-09-01 15:15 UTC (permalink / raw)
To: zsh-users
zzapper wrote:
> Hi
> I want to list the NEXT line after a string appears in a list of files
>
> eg
> > egrep "Anecdote" m??.txt (But display next line)
>
> I guess Sed/Awk would do this, I would probably use Perl.
>
> Suggestions pls
>
Note zsh specific, but this should work.
egrep -A1 "Anecdote" m??.txt | tail -1
--
John Eikenberry
[jae@zhar.net - http://zhar.net]
______________________________________________________________
"It is difficult to produce a television documentary that is both incisive
and probing when every twelve minutes one is interrupted by twelve dancing
rabbits singing about toilet paper." - Rod Serling
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 15:15 ` John Eikenberry
@ 2006-09-02 13:59 ` zzapper
0 siblings, 0 replies; 9+ messages in thread
From: zzapper @ 2006-09-02 13:59 UTC (permalink / raw)
To: zsh-users
John Eikenberry <jae@zhar.net> wrote in news:20060901151507.GA32421
@mollari.zhar.net:
> zzapper wrote:
>
>> Hi
>> I want to list the NEXT line after a string appears in a list of files
>>
>> eg
>> > egrep "Anecdote" m??.txt (But display next line)
>>
>> I guess Sed/Awk would do this, I would probably use Perl.
>>
>> Suggestions pls
>>
>
> Note zsh specific, but this should work.
> egrep -A1 "Anecdote" m??.txt | tail -1
John
That's useful I thought I knew everything about grep but no!
BTW works without the tail
egrep -iA2 "^Anecdote" m??.txt
--
http://successtheory.com/tips/ Vim, Zsh, MySQL Tips
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 14:59 I want to list the NEXT line after a string appears in a list of files zzapper
2006-09-01 15:15 ` John Eikenberry
@ 2006-09-01 15:15 ` Frank Terbeck
2006-09-02 18:41 ` zzapper
2006-09-01 15:27 ` Roman Cheplyaka
` (2 subsequent siblings)
4 siblings, 1 reply; 9+ messages in thread
From: Frank Terbeck @ 2006-09-01 15:15 UTC (permalink / raw)
To: zsh-users
zzapper <david@tvis.co.uk>:
> I want to list the NEXT line after a string appears in a list of files
>
> eg
> > egrep "Anecdote" m??.txt (But display next line)
>
> I guess Sed/Awk would do this, I would probably use Perl.
>
> Suggestions pls
[snip]
zsh% cat test0.txt
These
are
lines,
I
do
not
care
about.
but
please
display this line.
zsh% cat test1.txt
and please, oh please:
display this line, too.
not
this
one
zsh% function my_grep () {
local integer found=0
while read line ; do
(( found == 1 )) && print ${line} && return
(( found == 0 )) && [[ ${line} = *${1}* ]] && found=1
done < ${2}
}
zsh% for file in test*.txt ; do my_grep 'please' $file ; done
display this line.
display this line, too.
[snap]
Regards, Frank
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 15:15 ` Frank Terbeck
@ 2006-09-02 18:41 ` zzapper
0 siblings, 0 replies; 9+ messages in thread
From: zzapper @ 2006-09-02 18:41 UTC (permalink / raw)
To: zsh-users
Frank Terbeck <ft@bewatermyfriend.de> wrote in news:20060901151517.GC18634
@fsst.voodoo.lan:
> zzapper <david@tvis.co.uk>:
>> I want to list the NEXT line after a string appears in a list of files
All suggestions were excellent, in the end my preferred solution was the
Perl Script, as it was the easiest to alter when it turned I needed to
specify additional criteria.
Cheers
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 14:59 I want to list the NEXT line after a string appears in a list of files zzapper
2006-09-01 15:15 ` John Eikenberry
2006-09-01 15:15 ` Frank Terbeck
@ 2006-09-01 15:27 ` Roman Cheplyaka
2006-09-01 15:36 ` zzapper
2006-09-01 15:28 ` Chris Johnson
2006-09-01 16:52 ` Marc Chantreux
4 siblings, 1 reply; 9+ messages in thread
From: Roman Cheplyaka @ 2006-09-01 15:27 UTC (permalink / raw)
To: zsh-users
On Fri, Sep 01, 2006 at 02:59:03PM +0000, zzapper wrote:
> Hi
> I want to list the NEXT line after a string appears in a list of files
>
> eg
> > egrep "Anecdote" m??.txt (But display next line)
>
> I guess Sed/Awk would do this, I would probably use Perl.
--8<--
#!/usr/bin/perl
use strict;
my $regex = qr/Anecdote/;
my $print = 0;
while (<>)
{
$print && print;
$print = /$regex/;
}
--8<--
But, are you sure you've posted to the right place?..
--
Roman I. Cheplyaka
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 15:27 ` Roman Cheplyaka
@ 2006-09-01 15:36 ` zzapper
0 siblings, 0 replies; 9+ messages in thread
From: zzapper @ 2006-09-01 15:36 UTC (permalink / raw)
To: zsh-users
Roman Cheplyaka <roman.cheplyaka@gmail.com> wrote in
news:20060901152747.GB5849@localdomain:
> On Fri, Sep 01, 2006 at 02:59:03PM +0000, zzapper wrote:
>> Hi
>> I want to list the NEXT line after a string appears in a list of files
>>
>> eg
>> > egrep "Anecdote" m??.txt (But display next line)
> But, are you sure you've posted to the right place?..
>
That's a relevant point, just wondered if there might be a shell/zsh
solution/angle
--
http://successtheory.com/tips/ Vim, Zsh, MySQL Tips
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 14:59 I want to list the NEXT line after a string appears in a list of files zzapper
` (2 preceding siblings ...)
2006-09-01 15:27 ` Roman Cheplyaka
@ 2006-09-01 15:28 ` Chris Johnson
2006-09-01 16:52 ` Marc Chantreux
4 siblings, 0 replies; 9+ messages in thread
From: Chris Johnson @ 2006-09-01 15:28 UTC (permalink / raw)
To: zzapper; +Cc: zsh-users
zzapper sent me the following 0.3K:
> I want to list the NEXT line after a string appears in a list of files
>
> eg
> > egrep "Anecdote" m??.txt (But display next line)
>
> I guess Sed/Awk would do this, I would probably use Perl.
>
> Suggestions pls
cat file | awk 'BEGIN {print_next = 0;} {if (print_next) {print $0;
print_next = 0;}} /PATTERN/ {print_next = 1;}'
Here's an awk solution. If your line matches PATTERN, it sets a flag.
When the next record is read, the flag is evaluated. If set, the record
is printed.
This solution allows for a single line to both be printed and match.
--
Chris Johnson
cjohnson@cs.utk.edu
http://www.cs.utk.edu/~cjohnson
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: I want to list the NEXT line after a string appears in a list of files
2006-09-01 14:59 I want to list the NEXT line after a string appears in a list of files zzapper
` (3 preceding siblings ...)
2006-09-01 15:28 ` Chris Johnson
@ 2006-09-01 16:52 ` Marc Chantreux
4 siblings, 0 replies; 9+ messages in thread
From: Marc Chantreux @ 2006-09-01 16:52 UTC (permalink / raw)
To: zsh-users
le 01/09/2006,
zzapper nous écrivait :
> Hi
> I want to list the NEXT line after a string appears in a list of files
choose your destiny :
- little files : zsh
- medium : sed/awk (1)
- big : perl
(1) every one but the GNU one which is damn slow !
echo '
find a
find a
find a
find a
pattern
x
and print it
' > file
print sed:
sed -n '/pattern/ { # if pattern found
n # read next line
p # print the current line
}' < file
print wich can be written as:
sed -n '/pattern/{n;p}' < file
print perl:
perl -lne 'print $_=<> if /pattern/' file
print awk:
awk '/pattern/ { getline ; print }' file
print zsh -----------------
print first occurence :
# 1. store the content of file, line by line
content=( ${(f)"$( < file)"} )
# find a line containing your pattern
# > ${content[(i)*pattern*]}
# and print the next one
print ${content[${content[(i)*pattern*]} + 1]}
print all occurences :
while {read} {
[[ $REPLY == *pattern* ]] &&
read line &&
print $line
} < file
regards
--
téléphone : 03.90.24.00.19
courriel : marc.chantreux@ulpmm.u-strasbg.fr
---------------------------------------
^ permalink raw reply [flat|nested] 9+ messages in thread