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,

Can you post a sample of input and desired output?

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

sed -n '/MMM/{N;N;N;N;N;N;N;N;N;N;p;}' Inp_File | paste -d, - - - - - - - - - - -
1 Like

Adjust FindString and l as necessary.

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

A minor tweak to Shell_Life's solution which eliminates the need for paste:

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

Thanks for your help.