sumoka
March 21, 2011, 1:15pm
1
Hi,
I have a query as follows :
suppose I am matching a string in a file say "start from here"
and I want to pick up 'n' number of lines () from the matched string. Is there any way to do that ?
1) going forward I want to do this for every match for the above string
2) or limit this to only 1 occurrence/ 'n' occurences
Thanks in advance.
"n" lines from a match, this is one way with awk.
lines=3
awk -v lines=$lines '/start from here/ {n=lines} n {n--;print}' myfile > newfile
sumoka
March 23, 2011, 11:47am
3
Thanks!
but in my case it did not work. I ran it on the following para from wiki abt grep
There are countless implementations and derivatives of grep available for many operating systems. Early variants of grep included egrep and fgrep. egrep applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. fgrep searches for any of a list of fixed strings using the Aho.Corasick string matching algorithm. These variants of grep persist in most modern grep implementations as command-line switches (and standardized as -E and -F in POSIX[3]). In such combined implementations, grep may also behave differently depending on the name by which it is invoked, allowing fgrep, egrep, and grep to be links to the same program.
Other commands contain the word "grep" to indicate that they search (usually for regular expression matches). The pgrep utility, for instance, displays the processes whose names match a given regular expression.
In Perl, grep is the name of the built-in function that finds elements in a list that satisfy a certain property. This higher-order function is typically named filter in functional programming languages.
pcregrep is an implementation of grep that uses Perl regular expression syntax.
Ports of grep (within Cygwin and GnuWin32, for example) also run under Microsoft Windows. Some versions of Windows feature the similar qgrep command
stored it in a file names "Text"
and ran..
lines=5
awk -v ln=$lines '/may also behave/ {n=ln} n {n--;print}' Text
is there a BEGIN required ?