grep N lines after match and then print them on 1 line each


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep N lines after match and then print them on 1 line each
# 1  
Old 09-26-2009
grep N lines after match and then print them on 1 line each

Hello
I have a silly question. I need to grep a match in text file
and then print 5 lines after it.
grep -A 5 .... do it.
OK
The next thing I can not handle is I need each output to be on 1 line
match line2 line3 line4 line5
match line2 line3 line4 line5
etc..

I will really appreciate your help.

Last edited by alekkz; 09-26-2009 at 05:18 PM.. Reason: err
# 2  
Old 09-26-2009
could you please show the sample of files, you want to act upon.
# 3  
Old 09-26-2009
This should work, I think...

Code:
COUNT=1
grep -A 5 pattern file | while read line
do
   CURRENT=`expr $COUNT % 6`
   if [ 0 -ne $CURRENT ]
   then
     # convert newlines to spaces if not the 6th line  
     tr "\n" " " < $line >> outputfile
   else
     # retain the newline when $COUNT % 6 = 0 -- every 6th line
     echo $line >> output.txt
   fi
   COUNT=$COUNT+1
done

by the way, your code, grep -A 5 will out put 6 lines. but your example shows the result of grep -A 4 (5 lines).
# 4  
Old 09-26-2009
Quote:
Originally Posted by sanjay.login
could you please show the sample of files, you want to act upon.

file.txt
----- cut ----
Code:
TARGET
13/11/08
20:43:21
POINT 1
MOVE 8
772102y64312417771
TARGET
13/11/08
21:10:01
POINT 2
MOVE 5
731623jjd12njhd

----- cut ----
this is the example.
i need to grep for the word TARGET and use next 5 lines like that:

Code:
TARGET 13/11/08 20:43:21 POINT 1 MOVE 8
TARGET 13/11/08 21:10:01 POINT 2 MOVE 5



---------- Post updated at 04:07 PM ---------- Previous update was at 03:46 PM ----------

it returns:
Code:
line 6: [: -ne: unary operator expected 
expr: non-numeric argument


Last edited by vgersh99; 09-26-2009 at 06:20 PM.. Reason: code tags, PLEASE!
# 5  
Old 09-26-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 6  
Old 09-26-2009
Here, this one works:
Code:
#!/bin/sh
# create the output file
touch outputfile
# set the var
COUNT=1
# grep and feed to loop
grep -A 5 TARGET testdata | while read line
do
   # set the line tracking var
   CURRENT=`expr $COUNT % 6`
   if [ 0 -ne $CURRENT ]  # are we on the 6th line?
   then
     # convert newlines to spaces if not the 6th line
     echo $line | tr "\n" " " >> outputfile
   else
     # retain the newline when $COUNT % 6 = 0 -- every 6th line
     echo $line >> outputfile
   fi
   # increment the counter
   COUNT=`expr $COUNT + 1`
done


Last edited by varontron; 09-26-2009 at 06:52 PM.. Reason: added comments to code
# 7  
Old 09-26-2009
hi allekz,

you can go for this code.

Code:
grep -A 4 TARGET file_name|sed '/--/d'|awk '{for (i=1; i <=2; i++) printf($i"%c",OFS)}'|sed 's/TARGET/\nTARGET/g'|tr -s " "

it is giving the out put

Code:
TARGET 13/11/08 20:43:21 POINT 1 MOVE 8
TARGET 13/11/08 21:10:01 POINT 2 MOVE 5



and near future i will be sending another good code
regards,
Sanjay

Last edited by sanjay.login; 09-26-2009 at 07:06 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 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

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

5. 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

6. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

7. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

8. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

9. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question