How to print different multiple lines after two patterns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print different multiple lines after two patterns?
# 1  
Old 01-21-2016
How to print different multiple lines after two patterns?

Hello,

I need to print some lines as explained below,
TXT example
Code:
1111
2222
3333
4444
5555
6666
7777
8888
6666
9999
1111
2222
3333
4444
5555
6666
7777
8888
6666
9999

Let's say I have two patterns 1111 and 6666 and one variable n (dynamic line numbers depending on file). Assume n=2 here, I want to print "2" lines after 1111 including the pattern 1111 and the second pattern line in the original order. The output should looks like,

Code:
1111
2222
3333--the above two line after first pattern
6666
6666
1111
2222
3333--the above two line after first pattern
6666
6666

I tried with grep -A2 -e '1111' -e '6666' TXT. It did the job partly right because it also prints two lines after the second pattern "6666", which are not wanted.

I thank you very much for your help!

Zhen
# 2  
Old 01-21-2016
Got Perl?

Code:
perl -ne '/1111/ and $n=3; /6666/ and $n=1; print if $n-- > 0 ' liuzhencc.file

Code:
1111
2222
3333
6666
6666
1111
2222
3333
6666
6666

# 3  
Old 01-21-2016
Thanks. I think perl is installed as default with Ubuntu 14. That's already very good to get job done with perl. Is that any possibility to use sed or awk? I suppose either sed or awk should be able to do this job. However, I cannot work it out.
# 4  
Old 01-21-2016
Like so?
Code:
awk '$0 ~ P1 {L = NR + n} $0 ~ P2; NR <= L ' n=2 P1=1111 P2=6666 file
1111
2222
3333
6666
6666
1111
2222
3333
6666
6666

# 5  
Old 01-21-2016
Quote:
Originally Posted by RudiC
Like so?
Code:
awk '$0 ~ P1 {L = NR + n} $0 ~ P2; NR <= L ' n=2 P1=1111 P2=6666 file
1111
2222
3333
6666
6666
1111
2222
3333
6666
6666

Many thanks. You are always come with a super concise, super smart script to solve the problem. May I kindly ask a further question on this problem? How to modified this script if I want to print "2" lines before 1111 including the pattern 1111 and the second pattern line in the original order. Then the output should look like,
Code:
1111-->only one line because there no line above 6666 
6666-->this line meets both patterns 
9999-->two lines before the first pattern '1111' 
1111
6666
6666

# 6  
Old 01-21-2016
Would that work?
Code:
awk '$1=="1111"{n=3} $1=="6666" && n<1{n=1} n-- > 0' liuzhencc.file

# 7  
Old 01-22-2016
The easiest way would be to use my above proposal and reverse input and output:
Code:
tac file | awk '$0 ~ P1 {L = NR + n} $0 ~ P2 || NR <= L ' n=2 P1=1111 P2=6666 | tac
1111
6666
6666
9999
1111
6666
6666

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

3. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

4. Shell Programming and Scripting

Print between multiple patterns

Hello Gurus, I have a file this Dir Path 1 Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields Dir Path2 Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource name"="DS name2";some other fields. Under each dir... (14 Replies)
Discussion started by: sirababu
14 Replies

5. Shell Programming and Scripting

Perl : to print the lines between two patterns

Hello experts, I have a text file from which I need to print all the lines between the patterns. Could anyone please help me with the perl script. names.txt ========= Badger Bald Eagle Bandicoot Bangle Tiger Barnacle Barracuda Basilisk Bass Basset Hound Beetle Beluga... (7 Replies)
Discussion started by: scriptscript
7 Replies

6. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

7. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

8. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

9. Shell Programming and Scripting

print lines which match multiple patterns

Hi, I have a text file as follows: 11:38:11.054 run1_rdseq avg_2-5 999988.0000 1024.0000 11:50:52.053 run3_rdrand 999988.0000 1135.0 128.0417 11:53:18.050 run4_wrrand avg_2-5 999988.0000 8180.5833 11:55:42.051 run4_wrrand avg_2-5 999988.0000 213.8333 11:55:06.053... (2 Replies)
Discussion started by: annazpereira
2 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies
Login or Register to Ask a Question