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
# 1  
Old 11-20-2009
Extracting N lines match number X of a pattern

Hi All,
as the title says I need to extract N lines after match number X of a pattern.
e.g.
111
xxx
xxx
111
yyy
yyy
111
www
www
111
zzz
zzz

I would like to extract the two lines after the second 111 occurrence.

I tried with grep but I didn't find any options to do that.
Any help?
Thank you,
Sarah
# 2  
Old 11-20-2009
Try:

Code:
sed -n '/111/ { n;N;p; }' filename

or

Code:
awk '/111/ { getline; getline; print; }' filename

or

Code:
grep -A2 111 filename

# 3  
Old 11-20-2009
grep didn't work

Probably I was not clear enough:

I need to extract X lines after the Nth occurrence of a pattern.

The patter cointains '/' character this gave probably problem on sed.

something like

1/1/1/1
qweqw
qweqw
qweqe
1/1/1/1
qewq
qewq
1/1/1/1
qwe
1/1/1/1
1/1/1/1
# 4  
Old 11-20-2009
bash
Code:
cnt=0
pat="111"
num_lines=2
while read -r line
do
    case "$line" in
        $pat) cnt=$(( cnt+1 ))
    esac
    if [ "$cnt" -eq 2 ] ;then
        for((i=1;i<="$num_lines";i++))
        do
            read line
            echo $line
        done
        break
    fi 
done < "file"

# 5  
Old 11-20-2009
Code:
#! /bin/sh
line="$(grep -n '111' inputfile | head -n 2 | tail -n 1 | cut -d: -f1)"
let start=line+1
let end=start+1
sed -n "$start,$end"p inputfile

# 6  
Old 11-20-2009
Quote:
Originally Posted by f_o_555
grep didn't work

Probably I was not clear enough:

I need to extract X lines after the Nth occurrence of a pattern.

The patter cointains '/' character this gave probably problem on sed.

something like

1/1/1/1
qweqw
qweqw
qweqe
1/1/1/1
qewq
qewq
1/1/1/1
qwe
1/1/1/1
1/1/1/1
To get the 2 lines after the 2nd match of the pattern "1/1/1/1":

Code:
awk '/1\/1\/1\/1/ && ++c==2{p=2;next}p-->0' file

# 7  
Old 11-20-2009
thank you, now I have another problem some 0x0C characters in some lines.
In this way the line not read!!!

---------- Post updated at 05:57 AM ---------- Previous update was at 05:41 AM ----------

Quote:
Originally Posted by Franklin52
To get the 2 lines after the 2nd match of the pattern "1/1/1/1":

Code:
awk '/1\/1\/1\/1/ && ++c==2{p=2;next}p-->0' file

we're almost there with awk
it works with numeric values but not with variables, e.g.

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

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