Want to sort a file using awk & sed to get required output

Hi All, Need Suggestion, Want to sort a file using awk & sed to get required, output as below, such that each LUN shows correct WWPN and FA port Numbers correctly:
Required output:

01FB 10000000c97843a2   8C  0
01FB 10000000c96fb279    9C  0
22AF 10000000c97843a2   8C  0
22AF 10000000c975adbd   8C  0 
22AF 10000000c96fb279    9C  0
22AF 10000000c9720873   9C  0
22AF 10000000c95d5d8b   9C  0

please see below: I have sorted file A.txt some what using grep and sed as "| grep -v Device | grep -v Symmetrix | sed s/-//g", getting output B.txt, Required output is as above sorted

A.txt
Symmetrix ID : 0001901
Device Identifier Type Dir:P 
------ ---------------- ----- ---------------- 
01FB 10000000c97843a2 FIBRE FA-8C:0
10000000c96fb279 FIBRE FA-9C:0
 
Symmetrix ID : 0001901
Device Identifier Type Dir:P 
------ ---------------- ----- ---------------- 
22AF 10000000c97843a2 FIBRE FA-8C:0
10000000c975adbd FIBRE FA-8C:0
10000000c96fb279 FIBRE FA-9C:0
10000000c9720873 FIBRE FA-9C:0
10000000c95d5d8b FIBRE FA-9C:0
B.txt
01FB 10000000c97843a2 FIBRE FA-8C:0
10000000c96fb279 FIBRE FA-9C:0 
 
22AF 10000000c97843a2 FIBRE FA-8C:0
10000000c975adbd FIBRE FA-8C:0
10000000c96fb279 FIBRE FA-9C:0
10000000c9720873 FIBRE FA-9C:0
10000000c95d5d8b FIBRE FA-9C:0

Hope I Got your requirement right.

Try.. assuming some points here

awk -F "FIBRE FA-|:| " '/FIBRE/{if($0 !~ /^1000/){s=$1;$1=$1}else{$1=s" "$1}}1' file | sort
1 Like
perl -alne '{if(/^\d/) {$val=$F[0];$F[3]=~s/.*?-(\w+):(\d)/$1 $2/;print "$F[0] $F[1] $F[3]";} else{
if(/^ +/){$F[2]=~s/.*?-(\w+):(\d)/$1 $2/;print "$val $F[0] $F[2]";next;}}}' input_file

Hope this helps..

1 Like

also try: add one more process after:

 | sed s/-//g
 | awk '/^ *$/{next;} length($1)!=4 { $0=l " " $0; } {l=$1 ; gsub("[:-]"," "); $3=$4="";} 1'
1 Like

But i also need the FA port too, Required:

01FB 10000000c97843a2   8C  0
01FB 10000000c96fb279    9C  0
22AF 10000000c97843a2   8C  0
22AF 10000000c975adbd   8C  0 

Output:

22B7 10000000c9720873   0
22B7 10000000c95d5d8b   0
22BB 10000000c97843a2   0
22BB 10000000c975adbd   0
22BB 10000000c96fb279   0
22BB 10000000c9720873   0
22BB 10000000c95d5d8b   0
22BF 10000000c97843a2   0
22BF 10000000c975adbd   0

should sed script be:

sed 's/-/ /g'

?

1 Like

Am using as below:

| grep -v Device | grep -v Symmetrix | sed s/-//g | awk '/^ *$/{next;} length($1)!=4 { $0=l " " $0; } {l=$1 ; gsub("[:-]"," "); $3=$4="";} 1'

What happens if sed is changed to:

sed 's/-/ /g'

?

Gives same output as below:
22B7 10000000c9720873   0
22B7 10000000c95d5d8b   0
22BB 10000000c97843a2   0
22BB 10000000c975adbd   0
Not showing FA ports as required output:
 
01FB 10000000c97843a2   8C  0
01FB 10000000c96fb279   9C  0
22AF 10000000c97843a2   8C  0
22AF 10000000c975adbd   8C  0

try this..

grep -v -E "Device|Symmetrix|^------" A.txt | awk -F "FIBRE FA-|:| " '/FIBRE/{if($0 !~ /^1000/){s=$1;$1=$1}else{$1=s" "$1}}1' | sort

Try:

awk -F" |-|:"  'NF==6  {tmp=$1}
                /FIBRE/ {print tmp,$(NF-4),$(NF-1),$NF}
               ' file
01FB 10000000c97843a2 8C 0
01FB 10000000c96fb279 9C 0
22AF 10000000c97843a2 8C 0
22AF 10000000c975adbd 8C 0
22AF 10000000c96fb279 9C 0
22AF 10000000c9720873 9C 0
22AF 10000000c95d5d8b 9C 0

You might need to use another awk version on your system; see posts in this forum.