1753555 Members
5292 Online
108795 Solutions
New Discussion юеВ

grep a paragraph ??

 
SOLVED
Go to solution
Tamer Shaalan
Regular Advisor

grep a paragraph ??

Hii everybody here,
is there a method to filter out (from a text format file) a whole paragraph containing a line with certain pattern(which I serach for) (not only the line)??? this will be too helpful for me in processing large data amounts.
Thanks.
Tamer
Success is a journey, not a destination
7 REPLIES 7
H.Merijn Brand (procura
Honored Contributor

Re: grep a paragraph ??

lt09:/tmp 106 > cat test.txt
jer khaerg heiurrgh errhg iuehrg ihreg uhe
erg iuuehr guhre iguhreui hough erhg oehr
jhrg oqhergo hero

h whrego hogh oerhorhjow h owrjt hojro ht
tkj wltrj rttjh rjtho ijrtph wjrthiwj rtoj
rthj owitr TEST jpijrtthpijwtrphjwpiotrh
tijworej otijwpttjoi tjh

itjwprtj prrtjthh pwrtjpwjrttphjwpthowr
irjtpwjtrp rptij rptjhpprtjh pritj wprt
jteypiwjpwt
lt09:/tmp 107 > perl -ne'BEGIN{$/="\n\n"}/TEST/&&print' test.txt
h whrego hogh oerhorhjow h owrjt hojro ht
tkj wltrj rttjh rjtho ijrtph wjrthiwj rtoj
rthj owitr TEST jpijrtthpijwtrphjwpiotrh
tijworej otijwpttjoi tjh

lt09:/tmp 108 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Karthik S S
Honored Contributor

Re: grep a paragraph ??

To my understanding a big text block without any line breaks are considered as paragraph (from text processor's point of view). can you post a sample file here?

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Pete Randall
Outstanding Contributor
Solution

Re: grep a paragraph ??

From Handy One-Liners for sed (attached):

# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'


Pete

Pete
john korterman
Honored Contributor

Re: grep a paragraph ??

Hi,
how about this:
# cat | awk '/./,/\n\n/' |awk '/TEST/'

regards,
John K.
it would be nice if you always got a second chance
Tamer Shaalan
Regular Advisor

Re: grep a paragraph ??

Hiii Pete
Thanks a million for your great amazing scripts !!!!!
It is easly done !
Tamer
Success is a journey, not a destination
agustinL
Occasional Visitor

Re: grep a paragraph ??

Assuming the data is structured so that it's always the line
before and after that you want you can make use of grep's -A (after) and
-B (before) switches to tell it to include the 1 line before the match
and 1 line after it:

For example i'm grepping physical device memory configuration in
dmedecode, I can use the Locator: as anchor point to paragraph i need to
grep.

dmidecode|grep -A 10 -B 8 "Locator:"

The output will be like this:

Memory Device
Array Handle: 0x1000
Error Information Handle: Not Provided
Total Width: 72 bits
Data Width: 64 bits
Size: 8192 MB
Form Factor: FB-DIMM
Set: 1
Locator: Board 1, DIMM 1A
Bank Locator: Not Specified
Type: DDR3
Type Detail: Synchronous
Speed: 1333 MHz
Manufacturer: Not Specified
Serial Number: Not Specified
Asset Tag: Not Specified
Part Number: Not Specified
Rank: 2
Configured Clock Speed: 800 MHz

--

--

---

 

Dennis Handly
Acclaimed Contributor

Re: grep a paragraph?

> grep's -A and -B

 

These are only available in gnu's grep.