print multiple lines with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers print multiple lines with awk
# 1  
Old 06-16-2009
Data print multiple lines with awk

Hi everyone!

I'm not new to Unix, but I've never used awk before.
I tried to look up this information on several sites and forums,
I also looked in the documentation but I haven't found a solution yet.

I would like to print the previous 3 lines before and the following 4 lines after the given pattern, including the line with the pattern.

Could you help me with this issue, please?

Thanks.
# 2  
Old 06-16-2009
# 3  
Old 06-17-2009
Code:
awk '/pattern/{
 for(i=d-3;i<=d;i++){ print a[i] }
 for(o=1;o<=4;o++){ getline;  print}
 delete a
 d=0
}
{ a[++d]=$0}' file

# 4  
Old 06-17-2009
Ghostdog74, nice solution. I just fixed it up to print out only the 3 lines before the pattern, the line with the search pattern, and the following 4 lines per OP request.
Code:
awk '/pattern/ {
     for ( i = d - 2; i <= d; i++ ) { print a[i] }
     print
     for ( o = 1; o <= 4; o++ ) { getline; print}
     delete a
     d=0
}
{ a[++d] = $0 }' file

For example if patterm is "FINDME" and file contains
Code:
no before 1
no before 2
before 1
before 2
before 3
FINDME
after 1
after 2
after 3
after 4
no after 1
no after 2

the output is
Code:
before 1
before 2
before 3
FINDME
after 1
after 2
after 3
after 4

# 5  
Old 06-17-2009
Its a generalized answerSmilie
Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=4 s="pattern" file

here you need to change only a and b to get more lines above and below the patternSmilie
# 6  
Old 06-17-2009
Please just provide the link, otherwise it looks like you are passing someone else's work as your own...
https://www.unix.com/302098992-post2.html
# 7  
Old 06-18-2009
Thank you guys for answering this question!

I managed to do it now! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

2. Shell Programming and Scripting

Print multiple lines on one line

Hi All, I have a file looks like: rst:singh:99.0.20-X86 2 rst:ACSI_SIN_SERVICES rst:singh:99.0.20-X86 2 rst:ACSI_BISI want to wrap 3rd col in one line and add variable value at start and ending of line and I wrote command: cat file | awk '{print $3}' | xargs > command.txt sed -e... (1 Reply)
Discussion started by: rakeshtomar82
1 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

Print the first line for each multiple lines

Hi all, i need help to extract each first line from multiple lines occurrences based on different patterns (name) starting from the fourth lines like follows:- // header 1 header 2 header 3 // no acc name score rank //... (2 Replies)
Discussion started by: redse171
2 Replies

5. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

(awk?) print multiple lines on one line

I have a log file something like ------- report 1 ------- date 27/01/13 time 08:00 records 1234 ------- report 2------- date 27/01/13 time 08:00 records 1239 ... I'd like output to show as report 1,date 27/01/13,time 08:00,records 1234 report 2,date 27/01/13,time... (6 Replies)
Discussion started by: gefa
6 Replies

8. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

9. Shell Programming and Scripting

Can't print multiple lines inside awk :(

Hi Friends, I have small issue with following code snippet. I am trying call one function inside awk in which the function inturn will echo few lines. However when i ran script its throwing an error saying "nawk: syntax error at source line 1". #!/bin/sh eval input=$@ while read... (3 Replies)
Discussion started by: Shahul
3 Replies

10. Shell Programming and Scripting

AWK print lines into multiple files

Hi, i have an input text file like this: Student 1 maths science = Student 2 maths science = Student 3 maths science i would like to print each student information into separate files, each student id is separated by "=". (1 Reply)
Discussion started by: saint2006
1 Replies
Login or Register to Ask a Question