Extracting N lines match number X of a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting N lines match number X of a pattern
# 8  
Old 11-20-2009
You can use the variables this way:

Code:
awk -v occ=$OCCURRENCE -v nlines=$OCCURRENCE '/1\/1\/1\/1/ && ++c==occ{p=nlines;next}p-->0' file

# 9  
Old 11-20-2009
Quote:
Originally Posted by Franklin52
You can use the variables this way:

Code:
awk -v occ=$OCCURRENCE -v nlines=$OCCURRENCE '/1\/1\/1\/1/ && ++c==occ{p=nlines;next}p-->0' file

One more thing, can also the string be a variable in awk?
The string may contain /
so I guess something like

Code:
awk -v occ=$OCCURRENCE -v nlines=$OCCURRENCE '/$STRING/ && ++c==occ{p=nlines;next}p-->0' file

[/QUOTE] ?

Last edited by f_o_555; 11-20-2009 at 09:24 AM..
# 10  
Old 11-20-2009
Quote:
Originally Posted by f_o_555
One more thing, can also the string be a variable in awk?
The string may contain /
Something like:

Code:
awk -v var="1/1/1/1" '$0 ~ var && ++c==2{p=2;next}p-->0' file

# 11  
Old 11-21-2009
Using Perl:

Code:
$
$ cat -n f1
     1  1/1/1/1
     2  abcde
     3  fghij
     4  klmno
     5  1/1/1/1
     6  pqrs
     7  tuvw
     8  1/1/1/1
     9  xyz
    10  1/1/1/1
    11  ABCD
    12  EFGH
    13  1/1/1/1
    14  IJKLMNOP
$
$ # print 1 line after 3rd occurrence of "1/1/1/1"
$ perl -lne 'BEGIN{$O=3; $N=1; $P="1/1/1/1"} if (/$P/ && ++$o==$O){$x=1} elsif ($x && ++$n<=$N){print}' f1
xyz
$
$ # print 2 lines after 4th occurrence of "1/1/1/1"
$ perl -lne 'BEGIN{$O=4; $N=2; $P="1/1/1/1"} if (/$P/ && ++$o==$O){$x=1} elsif ($x && ++$n<=$N){print}' f1
ABCD
EFGH
$
$

tyler_durden
# 12  
Old 11-21-2009
-need to refresh first --
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Finding log files that match number pattern

I have logs files which are generated each day depending on how many processes are running. Some days it could spin up 30 processes. Other days it could spin up 50. The log files all have the same pattern with the number being the different factor. e.g. LOG_FILE_1.log LOG_FILE_2.log etc etc ... (2 Replies)
Discussion started by: atelford
2 Replies

3. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

4. UNIX for Dummies Questions & Answers

extracting lates pattern match from multiple matches in log

Hi, I have a large, multiline log file. I have used pcregrep to extract all entries in that log that match a particular pattern - where that pattern spans multiple lines. However, because the log file is large, and these entries occur every few minutes, I still output a very large amount... (6 Replies)
Discussion started by: dbrb2
6 Replies

5. Shell Programming and Scripting

Pattern to match decimal number and interger

Hi, i've a code if (($A && ((!($A =~ /^\d+$/))))) { -- -- not a number } else { --- its number. } which matches for integer value, i need to modify that pattern where it matches decimal number with 2 decimal points and also integer value. for eg: values 10, 10.00. 0.1, 1 , 3 must... (2 Replies)
Discussion started by: asak
2 Replies

6. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

7. UNIX for Dummies Questions & Answers

Extracting m lines after n lines after match

Hi All, I would like to extract from a text file m lines skipping n lines after a string occurrency. Is it possible with grep? e.g. qqq ww eee rrr ttt yyy uuu I want to print 2 lines skipping 1 line after the string 'ww' result would be rrr ttt (2 Replies)
Discussion started by: f_o_555
2 Replies

8. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

9. UNIX for Dummies Questions & Answers

Extracting lines that match string at certain position

I have a fixed length file in the following format <date><product_code><other data> The file size is huge and I have to extract only the lines that match a certain product code which is of 2 bytes length. I cannot use normal grep since that may give undesirable results. When I search for prod... (5 Replies)
Discussion started by: paruthiveeran
5 Replies

10. Shell Programming and Scripting

Extracting lines between 2 strings only if the pattern patches

Hi Friends, Please help me with the following problem: I have an xml file with the following lines: <init-param> <param-name>/default/directory</param-name> <param-value>default</param-value> </init-param> <init-param> ... (5 Replies)
Discussion started by: simran
5 Replies
Login or Register to Ask a Question