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?
# 1  
Old 07-03-2014
How to extract entire para instead of just line?

Hello,

I have a file with multiple paragraphs/sections each starting with word "Handle" and if I grep for a pattern, I should get contents of entire section/para (not just line). Please advise, thanks!

Code:
#script.sh file.txt "System Information"
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

Code:
#cat file.txt
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
                Runtime Size: 89376 bytes
                ROM Size: 64 kB
                Characteristics:
                        ISA is supported
                        PCI is supported
                        PC Card (PCMCIA) is supported
                        PNP is supported
                        APM is supported
                        BIOS is upgradeable
                        BIOS shadowing is allowed
                        ESCD support is available
                        USB legacy is supported
                        Smart battery is supported
                        BIOS boot specification is supported
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
Handle 0x0002
        DMI type 2, 15 bytes.
        Base Board Information
                Manufacturer: Intel Corporation
                Product Name: 440BX Desktop Reference Platform
                Version: None
                Serial Number: None
                Asset Tag: Not Specified
                Features: None
                Location In Chassis: Not Specified
                Chassis Handle: 0x0000
                Type: Unknown
                Contained Object Handlers: 0
Handle 0x0003
        DMI type 3, 21 bytes.
        Chassis Information
                Manufacturer: No Enclosure
                Type: Other
                Lock: Not Present
                Version: N/A
                Serial Number: None
                Asset Tag: No Asset Tag
                Boot-up State: Safe
                Power Supply State: Safe
                Thermal State: Safe
                Security Status: None
                OEM Information: 0x00001234
Heigth: Unspecified
Number Of Power Cords: Unspecified
                Contained Elements: 0

# 2  
Old 07-03-2014
An awk approach:
Code:
awk -v S="System Information" '
        {
                if ( $0 !~ /^ / )
                        T = $0
                if ( T )
                        A[T] = A[T] ? A[T] RS $0 : $0
        }
        END {
                for ( k in A )
                {
                        if ( A[k] ~ S )
                                print A[k]
        }       }
' file.txt


Last edited by Yoda; 07-03-2014 at 02:19 PM.. Reason: removed next
# 3  
Old 07-03-2014
Is this dmidecode output? Try:

Code:
dmidecode | awk '/System Information/' RS=

or try:
Code:
dmidecode -qt 0x0001

or
Code:
dmidecode -s system-manufacturer

etc..

Last edited by Scrutinizer; 07-03-2014 at 02:56 PM..
# 4  
Old 07-03-2014
Thanks Yoda and Scrutinizer.

@Yoda - Sorry your solution NOT working. It just prints System Information

Code:
# awk -v S="System Information" '
        {
                if ( $0 !~ /^ / )
                        T = $0
                if ( T )
                        A[T] = A[T] ? A[T] RS $0 : $0
        }
        END {
                for ( k in A )
                {
                        if ( A[k] ~ S )
                                print A[k]
        }       }
' /tmp/dmi_out
        System Information

@Scrutinzer - You are right. Actually I need to run dmidecode -t system which does NOT work on older RHEL 3 & 4 [ It works on RHEL 5.3 onwards ]. Hence Am trying to run just dmidecode and extract only the paragraph with "System Information".

-qt and -s options are also not working.

Please advise - thanks a lot!!

Last edited by reddyr; 07-03-2014 at 03:09 PM..
# 5  
Old 07-03-2014
Code:
$ sed 's/^[^[:space:]]/^&/' dmi.out | awk '/System Info/' RS=^
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

I spent a little while trying to get (GNU) awk to use a record separator of 'any line which doesn't start with a space', but failed.

Last edited by CarloM; 07-03-2014 at 03:46 PM.. Reason: More general sed
# 6  
Old 07-03-2014
Try not so good...

Code:
$ awk '/System Information/{print;while(getline){if(!/^[ \t]+/){exit}print}}' dmi.out
        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

# 7  
Old 07-04-2014
Put the below code in script.sh
Code:
awk '/^Handle/ {print ""}1' "${1}" | awk '$0 ~ pat' pat="${2}" RS=

and run the script as below
Code:
./script.sh file.txt "System Information"

output:
Code:
$ ./script.sh file.txt "System Information"
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
$

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