Reputation: 73
I get output from Junos Switches in such format:
Physical interface: ge-0/0/7, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 42210774942 1163342
I need only the interface name and dropped packets value in one line, something like this:
ge-0/0/7 - 1163342
I tried many different combination of sed
and awk
. I tried to merge 3 lines to get only the columns I need, but it did not work.
This is how I tried to merge lines:
cat file.txt.out | awk '{getline b; getline c;printf("%s %s %s\n",$0,b,c)}'
But it seems the resulting line is too long, so i was not able to get what is needed. Please help
The output looks like this(but much longer):
Physical interface: ge-0/0/0, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 4582206 0
Physical interface: ge-0/0/1, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 14419826529 112564
Physical interface: ge-0/0/2, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 67593521901 1675707
Physical interface: ge-0/0/3, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 44283738671 977315
Physical interface: ge-0/0/4, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 98998665742 5065245
Physical interface: ge-0/0/5, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 56932179711 1446413
Physical interface: ge-0/0/6, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 34955222648 578513
Upvotes: 0
Views: 457
Reputation: 203324
It's extremely hard to guess with just one block of sample input, but this may be what you want:
$ awk -v RS= '{print $3, $NF}' file
ge-0/0/7, 1163342
If not, post a few blocks of your input file instead of just one so we can get a better idea what it is you're trying to parse.
Given your newly posted sample input, this is what you need:
$ awk '(NR%3)==1{p=$3} (NR%3)==0{print p, $NF}' file
ge-0/0/0, 0
ge-0/0/1, 112564
ge-0/0/2, 1675707
ge-0/0/3, 977315
ge-0/0/4, 5065245
ge-0/0/5, 1446413
ge-0/0/6, 578513
Upvotes: 2
Reputation: 73
Ok, after almost 2 days of struggling (I am a beginner in unix scripting) i have got this:
cat myfile | sed -n -e '/Physical/,$p' | egrep -v 'Dropped|master' | awk '{gsub(/Physical interface:|Enabled,|Physical link is|0 N4M-Q1/,"")}1' | sed '/^\s*$/d' | sed -e 's/ \+/ /g' | xargs -n 4 | awk '{ print $1" "$4 }'
Which produces such result:
ge-0/0/0, 0
ge-0/0/1, 112564
ge-0/0/2, 1675707
ge-0/0/3, 977315
ge-0/0/4, 5065245
ge-0/0/5, 1446413
ge-0/0/6, 578513
ge-0/0/7, 1163342
ge-0/0/8, 1297
ge-0/0/9, 1604987
I realize that this soution might not be the most optimal, but at least it does what i need;) "Optimization" proposals are appreciated:)
Upvotes: 0
Reputation: 188014
Totally fragile:
$ cat test.txt
Physical interface: ge-0/0/7, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 42210774942 1163342
$ echo $(grep -Po "Physical interface: \K[^,]*" test.txt) "-" \
$(awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt)
ge-0/0/7 - 1163342
This works like this:
grep -Po "Physical interface: \K[^,]*" test.txt
get all the text after Physical interface:
and up to a comma.awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt
in those lines not containing Phys
, nor Drop
nor being empty, print the last field.You can also use printf
to have more control over the content your are printing:
printf "%s - %s\n" $(grep -Po "Physical interface: \K[^,]*" test.txt) $(awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt)
Upvotes: 1