Kalin Borisov
Kalin Borisov

Reputation: 1120

Get values from two rows and convert to variables

I have values from two rows, want to get all values and make them to variables.

Output is from emc storage:

Bus 0 Enclosure 0  Disk 0
State:                   Enabled

Bus 0 Enclosure 0  Disk 1
State:                   Enabled

Expected result:

Bus:0|Enclosure:0|Disk:0|State:Enabled

Or just need somebody to give me direction how to get the last row ...

Upvotes: 0

Views: 94

Answers (4)

user000001
user000001

Reputation: 33377

Try this awk:

$ awk '/^Bus/{for(i=1;i<=NF;i+=2) printf "%s:%s|", $i,$(i+1)}/^State/{printf "%s%s\n", $1, $2}' file
Bus:0|Enclosure:0|Disk:0|State:Enabled
Bus:0|Enclosure:0|Disk:1|State:Enabled

To handle multiple words in the last field, you can do:

$ awk '/^Bus/{for(i=1;i<=NF;i+=2) printf "%s:%s|", $i,$(i+1)}/^State/{printf "%s", $1; for (i=2;i<=NF;i++) printf "%s ", $i; print ""}' file
Bus:0|Enclosure:0|Disk:0|State:Enabled 
Bus:0|Enclosure:0|Disk:1|State:hot space

Upvotes: 2

Chris Seymour
Chris Seymour

Reputation: 85865

With GNU awk you could do:

$ awk 'NR>1{$6=$6$7;NF--;print RS,$0}' RS='Bus' OFS='|' file
Bus|0|Enclosure|0|Disk|0|State:Enabled
Bus|0|Enclosure|0|Disk|1|State:Enabled

And for the last row only:

$ awk 'END{$6=$6$7;NF--;print RS,$0}' RS='Bus' OFS='|' file
Bus|0|Enclosure|0|Disk|1|State:Enabled

Upvotes: 1

potong
potong

Reputation: 58488

This might work for you (GNU sed):

sed '/^Bus/!d;N;s/[0-9]\+/:&|/g;s/\s//g' file

To get only the last row:

sed '/^Bus/{N;h};$!d;x;s/[0-9]\+/:&|/g;s/\s//g' file

Upvotes: 3

mpapec
mpapec

Reputation: 50667

perl -00anE 's/:// for @F; say join "|", map { $_%2 ? () : "$F[$_]:$F[$_+1]" } 0..$#F' file

output

Bus:0|Enclosure:0|Disk:0|State:Enabled
Bus:0|Enclosure:0|Disk:1|State:Enabled

Upvotes: 2

Related Questions