Sunday, October 24, 2021

[SOLVED] grep multiple column and field

Issue

i have this file https://app.box.com/s/qbcvzepzhv1gjbbx9f6fu9by0fsfbr0m

i tried to grep with

awk '{print $1 $3 $14}' 1.txt

but the output like this

=========================Streams

SrcaddrDelta(ms)

192.168.0.104192.168.0.101

192.168.0.102192.168.0.1010.49

192.168.0.101192.168.0.1040.49

192.168.0.101192.168.0.1023.54

==============================================================

the first value of mean jitter is missing.

and then. i want the output like this:

src ip addr | dest ip addr | mean jitter(ms)

192.168.0.104 | 192.168.0.101 | 3.53

192.168.0.102 | 192.168.0.101 | 0.49

192.168.0.101 | 192.168.0.104 | 0.49

192.168.0.101 | 192.168.0102 | 3.54

pls help me. sry my bad english


Solution

The problem: The source data doesn't have proper field separator and fields contains spaces. Therefore only using awk with $ position can't produce expected output.

E.g. your field name "Src IP addr" will translate to awk as $1=Src, $2=IP and $3=addr etc.

Solution:

$ sed '1,2 d; $d' 1.txt | awk 'NF = NF - 1 {print $1,"|",$3,"|",$NF}' | awk 'BEGIN{print "src ip addr | dest ip addr | mean jitter(ms)"}1'
src ip addr | dest ip addr | mean jitter(ms)
192.168.0.104 | 192.168.0.101 | 3.53
192.168.0.102 | 192.168.0.101 | 0.49
192.168.0.101 | 192.168.0.104 | 0.49
192.168.0.101 | 192.168.0.102 | 3.54

In details:

Command: sed '1,2 d; $d' 1.txt
Deletes first (1d) and last ($d) line as not useful along with original header Src IP addr Port Dest IP addr.... because there are too many whitespaces to deal with.

Command: awk 'NF = NF - 1 {print $1,"|",$3,"|",$NF}'
Here we solve the first problem you mentioned: "the first value of mean jitter is missing."
Since your SSRC field contains values with spaces that's why you didn't get the 1st value of mean jitter(ms).
To hack this, awk have NF (last field) you can manipulate. With your case NF would have equal to Problems column. So NF = NF - 1 gives new value by redefining last field sequence number and Mean Jitter(ms) column rows become as new NF.

Command: awk 'BEGIN{print "src ip addr | dest ip addr | mean jitter(ms)"}1'
Add new header to match expected result.



Answered By - Kristo Mägi