Grep and print next 10 Lines separated by ,


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and print next 10 Lines separated by ,
# 1  
Old 09-09-2011
Grep and print next 10 Lines separated by ,

Hi All,

I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl.

Any suggesstions please.

Note; I dont have a GNU Grep to use -A flag.

Thanks,
# 2  
Old 09-09-2011
Can you post a sample of input and desired output?
# 3  
Old 09-09-2011
Input:
FindString
Line after string
Line after string
Line after string
Line after string
----100Lines
Line after string
FindString
Line after string
Line after string
Line after string
Line after string
----100Lines
Line after string
FindString
Line after string
Line after string
Line after string
Line after string
----100Lines

Output:
FindString,Line after string,Line after string,----,10th Line after string
FindString,Line after string,Line after string,----,10th Line after string
FindString,Line after string,Line after string,----,10th Line after string

I need to grep all the occurenses of FindString and print first 10 Lines for each occurence
# 4  
Old 09-09-2011
Code:
sed -n '/MMM/{N;N;N;N;N;N;N;N;N;N;p;}' Inp_File | paste -d, - - - - - - - - - - -

This User Gave Thanks to Shell_Life For This Post:
# 5  
Old 09-09-2011
Adjust FindString and l as necessary.

Code:
awk '/FindString/ { l=10; } l&&l-- {printf("%s%s", $0, l?",":"\n");}' file

These 2 Users Gave Thanks to neutronscott For This Post:
# 6  
Old 09-09-2011
Quote:
Originally Posted by Shell_Life
Code:
sed -n '/MMM/{N;N;N;N;N;N;N;N;N;N;p;}' Inp_File | paste -d, - - - - - - - - - - -

A minor tweak to Shell_Life's solution which eliminates the need for paste:
Code:
sed -n '/MMM/{N;N;N;N;N;N;N;N;N;N;s/\n/,/g;p;}'

Something to bear in mind is that since this is not GNU sed, it may be necessary to qualify those N commands with $! (POSIX and most seds discard the pattern space if N tries to read a line when the EOF has been reached). However, this would only be necessary if the pattern matches a line that is followed by less than 10 lines and if the correct solution must print those lines. Given what we've been told, it's unknown whether or not this scenario is even a possibility.

Regards,
Alister
# 7  
Old 09-10-2011
Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print remaining lines using grep

Hi All, I am having a text file like below ERROR - Not a valid ID : 123 ERROR - Not a valid hello ID : 124 SUCCESS - Valid ID : 12 I need to display like below after reading the file if it finds the error keyword along with displaying this first line when error pattern... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

2. Shell Programming and Scripting

Grep for the string and then print the next 4 lines

RHEL 5.8 I have a text file like below. I want to grep for a string and then print the next 4 lines including the line with the string I grepped for For eg: I want grep for the string HANS and then print the next 4 lines including HANS $ cat someText.txt JOHN NATIONALITY:... (7 Replies)
Discussion started by: omega3
7 Replies

3. Shell Programming and Scripting

grep lines separated with semicolon

Hello, I would like to kindly ask you for help. I have a file with some lines in one row separated by semicolon. I need to find out, if the line I have in different variable is included in this file. e.g I have a file foo.txt with lines A=hello there;hello world;hello there world In... (6 Replies)
Discussion started by: satin1321
6 Replies

4. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

5. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

6. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question