grep from 3 lines and sort


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep from 3 lines and sort
# 1  
Old 11-12-2012
grep from 3 lines and sort

HTML Code:
Pseudo name=hdiskpower54
Symmetrix ID=000190101757
Logical device ID=0601
state=alive; policy=SymmOpt; priority=0; queued-IOs=0
==============================================================================
---------------- Host ---------------   - Stor -   -- I/O Path -  -- Stats ---
###  HW Path                I/O Paths    Interf.   Mode    State  Q-IOs Errors
==============================================================================
   0 fscsi0                    hdisk105  FA  9cA   active  alive      0      0
   1 fscsi4                    hdisk206  FA  8cA   active  alive      0      0

Pseudo name=hdiskpower55
Symmetrix ID=000190101757
Logical device ID=0605
state=alive; policy=SymmOpt; priority=0; queued-IOs=0
==============================================================================
---------------- Host ---------------   - Stor -   -- I/O Path -  -- Stats ---
###  HW Path                I/O Paths    Interf.   Mode    State  Q-IOs Errors
==============================================================================
   0 fscsi0                    hdisk106  FA  9cA   active  alive      0      0
   1 fscsi4                    hdisk207  FA  8cA   active  alive      0      0

Pseudo name=hdiskpower56
Symmetrix ID=0001901231133
Logical device ID=0609
state=alive; policy=SymmOpt; priority=0; queued-IOs=0
.
.
.
From the file above, I like to grep 3 lines:
HTML Code:
Pseudo name=hdiskpower54
Symmetrix ID=000190101757
Logical device ID=0601
and display in horizontal format so that it shows up

HTML Code:
Symmetrix ID       Pseudo name           Logical device ID 
000190101757     hdiskpower54           0601
..
..
Also, it will be great to sort by Symmetrix ID.

Please advise.
# 2  
Old 11-12-2012
Code:
awk -F"=" ' BEGIN {
 print "Pseudo name Symmetrix ID Logical device ID";
} /(Symmetrix ID|Pseudo name|Logical device ID)/ { si=$NF;getline;pn=$NF;getline;ld=$NF; print si, pn, ld; } ' input_file
Pseudo name Symmetrix ID Logical device ID
hdiskpower54 000190101757 0601
hdiskpower55 000190101757 0605
hdiskpower56 0001901231133 0609

# 3  
Old 11-12-2012
AWK's multiline-record handling can make short work of this task. Set RS to a null string, FS to an equals sign, and you can then access all pertinent values using field variables ($1, $2, etc).

What have you tried?

Regards,
Alister
# 4  
Old 11-12-2012
Code:
awk -F= '/^Pseudo/{++c;P[c]=$2;getline;S[c]=$2;getline;L[c]=$2}END{i=0;while(++i in P){print P[i],S[i],L[i]}}' OFS="\t" yourfile

# 5  
Old 11-12-2012
Thank you so much all!
It works fantastic!
# 6  
Old 11-12-2012
With the sample data, it doesn't make any difference, but it looks like the solutions provided so far ignored the request for sorted output. The following should do what was requested:
Code:
awk -F = 'BEGIN {OFS = "\t"}
/name=/{P = $2}
/x ID=/{S = $2}
/e ID=/{L = $2
        if(c++ == 0) printf("Symmetrix ID\tPseudo name\tLogical device ID\n")
        print S, P, L | "sort -n"
}' file

For those who insist on a single-line version, the following is a logical equivalent, although not as easy to read:
Code:
awk -F = 'BEGIN{OFS="\t"}/name=/{P=$2}/x ID=/{S=$2}/e ID=/{L=$2;if(c++==0)printf("Symmetrix ID\tPseudo name\tLogical device ID\n");print S,P,L|"sort -n"}' file

Neither of these will print the header unless at least one data line will also be printed.
# 7  
Old 11-12-2012
As alister suggests:
Code:
awk -F= 'NR==1{print $3,$1,$5} {print $4,$2,$6}' OFS='\t' RS= infile

or to sort:
Code:
awk -F= '{print $4,$2,$6}' OFS='\t' RS= infile | sort -n


Last edited by Scrutinizer; 11-12-2012 at 04:24 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

2. Homework & Coursework Questions

awk with Grep and Sort

1. The problem statement, all variables and given/known data: Please bare in mind I am a complete novice to this and have very very basic knowledge so please keep any answers as simple as possible and explain in terms I will understand ahha :):) I have a text file of names and test scores... (1 Reply)
Discussion started by: jamesb18
1 Replies

3. Homework & Coursework Questions

Grep and Sort

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1. Print the number of people that are in the /etc/passwd file with the name of George 2. Sort by name and... (8 Replies)
Discussion started by: Jagst3r21
8 Replies

4. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

5. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

6. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 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

how to grep sort userids

hello folks i have a file that have data like /test/aa/123 /test/aa/xyz /test/bb/xyz /test/bb/123 in above lines i just wants to grep "aa" and "bb". Thanks, Bash (4 Replies)
Discussion started by: learnbash
4 Replies

10. UNIX for Dummies Questions & Answers

Sort/Grep Question

Hello all, I have a test file that has the format: ..... O 3.694950 -.895050 1.480000 O 5.485050 .895050 1.480000 Ti -4.590000 4.590000 2.960000 Ti -2.295000 ... (5 Replies)
Discussion started by: aarondesk
5 Replies
Login or Register to Ask a Question