How to extract entire para instead of just line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract entire para instead of just line?
# 8  
Old 07-04-2014
Try:
Code:
awk '!/^ /{if(f)exit; p=x} /System Information/{f=1} {p=p $0 ORS} END{if(f)print p}' file

or

Code:
awk '!/^ /{if(f)exit; p=x} $0~s{f=1} {p=p $0 ORS} END{if(f)print p}' s="System Information" file


Last edited by Scrutinizer; 07-04-2014 at 02:04 AM..
# 9  
Old 07-04-2014
Another approach
Paste the below code in your script.sh
Code:
awk '/^Handle/ {if(p ~ pat) print p; p=$0; next} {p = p "\n" $0} END {if(p ~ pat) print p}' pat="${2}" "${1}"

# 10  
Old 07-05-2014
Hi.

Command cgrep allows this kind of extraction, for example:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate "paragraph" extraction, dmidecode output, cgrep
# For cgrep source, see:
# http://sourceforge.net/projects/cgrep/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}
lines=$( wc -l < $FILE )

pl " Part of input data file $FILE, $lines total lines:"
head $FILE

pl " Expected output:"
cat expected-output.txt

pl " Results:"
cgrep -D +w "^Handle" -w "^Handle" +I2 "System Information" $FILE

exit 0

producing:
Code:
./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
cgrep ATT cgrep 8.15

-----
 Part of input data file data1, 63 total lines:
SMBIOS 2.4 present.
98 structures occupying 3681 bytes.
Table at 0x000E0010.
Handle 0x0000
        DMI type 0, 24 bytes.
        BIOS Information
                Vendor: Phoenix Technologies LTD
                Version: 6.00
                Release Date: 04/15/2011
                Address: 0xEA2E0

-----
 Expected output:
Handle 0x0001
        DMI type 1, 27 bytes.
        System Information
                Manufacturer: VMware, Inc.
                Product Name: VMware Virtual Platform
                Version: None
                Serial Number: VMware-50 2a 97 c6 3e 64 b4 f5-ec 9f 59 78 f3 5d                                                                                         13 6a
                UUID: 502A97C6-3E64-B4F5-EC9F-5978F35D136A
                Wake-up Type: Power Switch

-----
 Results:
Handle 0x0001
        DMI type 1, 27 bytes.
        System Information
                Manufacturer: VMware, Inc.
                Product Name: VMware Virtual Platform
                Version: None
                Serial Number: VMware-50 2a 97 c6 3e 64 b4 f5-ec 9f 59 78 f3 5d                                                                                         13 6a
                UUID: 502A97C6-3E64-B4F5-EC9F-5978F35D136A
                Wake-up Type: Power Switch

The cgrep command needs to be compiled. See the comment in the script for location.

Best wishes ... cheers, drl
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 extract entire stanza using awk?

Hello friends, I have a text file with lot of stanzas with each starting with "O-O-O. Sample file :- 1. d4 Nf6 2. c4 g6 3. Nc3 d5 4. cxd5 Nxd5 5. e4 Nxc3 6. bxc3 Bg7 7. Nf3 c5 8. Rb1 O-O 9. Be2 cxd4 10. cxd4 Qa5+ 11. Bd2 Qxa2 12. O-O Bg4 13. Bg5 h6 14. Be3 (8 Replies)
Discussion started by: prvnrk
8 Replies

2. Shell Programming and Scripting

Using the entire line with space in between

Hi Folks, I have a report data like the one seen below. FRUITS@NEW_ORANGE(1500 04/29) FRUITS@NEW_ORANGE(1500 05/04) FRUITS@NEW_ORANGE(1500 05/05) FRUITS@NEW_ORANGE(1500 05/07) FRUITS@NEW_ORANGE(1500 05/12) I need to use each of this lines separately in another for loop like the one... (2 Replies)
Discussion started by: jayadanabalan
2 Replies

3. Shell Programming and Scripting

Help with sed to replace entire line

Hi, I need to replace an entire mailx line as follows using sed: sed -e 's/<line1>/<newline>/g' <filename> But I am getting comman garbled error since the new line has many special characters. I enclosed allspecial chars in \ but still no use. Can any one help me? Please use code... (2 Replies)
Discussion started by: vinodhin4
2 Replies

4. Shell Programming and Scripting

sed and awk to insert a line after a para

hi I am having a file like this ############################## mod1 ( a(ll) , b( c), try(o) , oll(ll) go(oo) , al(ll) mm(al) , lpo(kka) kka(oop) ); mod2 ( jj(ll) , c( kk), try1q(o1) , ofll(lll) gao(oo1) , ala(llaa) mmf(adl) , lddpo(kkad) kkda(oodp) );... (20 Replies)
Discussion started by: kshitij
20 Replies

5. Shell Programming and Scripting

Script required to extract a specific snippet from the entire file.

Hi, I have a file with the following structure. XXXXX........... YYYYY........... ................. .................. ZZZZZZ...... qwerty_start.............. .................. ................. .................. querty_end................ .............................. (1 Reply)
Discussion started by: abinash
1 Replies

6. UNIX for Dummies Questions & Answers

Show the entire line using ps

Using the vanilla ps -ef I noticed that the CMD (or command) line gets cut off after 90 characters UID PID PPID C STIME TTY TIME CMD root 6020 3822 0 Jun 19 ? 0:01 ./webservd-wdog -r /export/opt/sows/61sp4 -d /export/opt/sows/61sp4/https-logse Googling... (4 Replies)
Discussion started by: SixSigma1978
4 Replies

7. Shell Programming and Scripting

Replace entire line

I want to replace one line from my configuration file with the new settings. The file Name: /etc/httpd/conf/httpd.conf The following line should be replaced with the line mentioned below. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "\"%h\"... (3 Replies)
Discussion started by: shantanuo
3 Replies

8. Shell Programming and Scripting

Print the entire line if second field has value P

Friends, I have .txt file with 3 millions of rows. File1.txt ABC1|A|ABCD1|XYZ1 ABC2|P|ABCD2|XYZ2 ABC3|A|ABCD3|XYZ3 ABC4|P|ABCD4|XYZ4 If second field has value P then print the entire line. Thanks in advance for your help, Prashant (4 Replies)
Discussion started by: ppat7046
4 Replies

9. UNIX for Dummies Questions & Answers

grep entire statement not just line

(extract from SQL binlog file...) # at 4960 #080801 14:35:31 server id 4 end_log_pos 195 Query thread_id=63121426 exec_time=0 error_code=0 use d_jds; SET TIMESTAMP=1217581531; UPDATE bid_details set bidding = 3170.37 ,deduction=if((3170.37 < 37.43),0,deduction) where... (3 Replies)
Discussion started by: shantanuo
3 Replies

10. Shell Programming and Scripting

Capture entire line in ps command

I need to determine what processes are running at certain times of the day. I have a script that issues the /usr/ucb/ps aux command and captures it to a file. I want to see the cpu usage and memory usage. This command lops off the end of the of the display line so I can't see the entire... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question