Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: I want to list the NEXT line after a string appears in a list of files



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@xxxxxxxxxxxxxxxxxx
---------------------------------------



Messages sorted by: Reverse Date, Date, Thread, Author