Solved

simple awk question

Posted on 2013-12-27
10
337 Views
Last Modified: 2013-12-28
I have 2 outputs a.txt and b.txt as given below

and i want to organize it like


Upload 2 697015680
IF-MIB::ifOutOctets.2 = Counter32: 697015680

Download 2 3584404
IF-MIB::ifInOctets.2 = Counter32: 3584404


it will crop the bold parts and re-organize it as given above and output  c.txt will be as this :

Upload 2 697015680
Download 2 3584404

Open in new window




how should i do it ?


IF-MIB::ifOutOctets.1 = Counter32: 0
IF-MIB::ifOutOctets.2 = Counter32: 697015680
IF-MIB::ifOutOctets.3 = Counter32: 1367120932
IF-MIB::ifOutOctets.4 = Counter32: 696749845
IF-MIB::ifOutOctets.5 = Counter32: 3548677295
IF-MIB::ifOutOctets.6 = Counter32: 647229040
IF-MIB::ifOutOctets.7 = Counter32: 738125034
IF-MIB::ifOutOctets.8 = Counter32: 2025915937
IF-MIB::ifOutOctets.9 = Counter32: 694844334
IF-MIB::ifOutOctets.10 = Counter32: 0
IF-MIB::ifOutOctets.11 = Counter32: 1478519182
IF-MIB::ifOutOctets.12 = Counter32: 3180382488
IF-MIB::ifOutOctets.13 = Counter32: 392389537
IF-MIB::ifOutOctets.14 = Counter32: 0
IF-MIB::ifOutOctets.15 = Counter32: 1663739512
IF-MIB::ifOutOctets.16 = Counter32: 0
IF-MIB::ifOutOctets.17 = Counter32: 1515634874
IF-MIB::ifOutOctets.18 = Counter32: 2216637904
IF-MIB::ifOutOctets.19 = Counter32: 1192467295
IF-MIB::ifOutOctets.20 = Counter32: 695983693
IF-MIB::ifOutOctets.21 = Counter32: 2423534788
IF-MIB::ifOutOctets.22 = Counter32: 3988280951
IF-MIB::ifOutOctets.23 = Counter32: 1160171512
IF-MIB::ifOutOctets.24 = Counter32: 1845170441
IF-MIB::ifOutOctets.25 = Counter32: 696876211
IF-MIB::ifOutOctets.26 = Counter32: 1362592945
IF-MIB::ifOutOctets.27 = Counter32: 694767198
IF-MIB::ifOutOctets.28 = Counter32: 694776997
IF-MIB::ifOutOctets.29 = Counter32: 0
IF-MIB::ifOutOctets.30 = Counter32: 694885985
IF-MIB::ifOutOctets.31 = Counter32: 3983940132
IF-MIB::ifOutOctets.32 = Counter32: 1083108854
IF-MIB::ifOutOctets.33 = Counter32: 2380886403
IF-MIB::ifOutOctets.34 = Counter32: 0
IF-MIB::ifOutOctets.35 = Counter32: 1461604829
IF-MIB::ifOutOctets.36 = Counter32: 2692517989
IF-MIB::ifOutOctets.37 = Counter32: 694865372
IF-MIB::ifOutOctets.38 = Counter32: 697669619
IF-MIB::ifOutOctets.39 = Counter32: 696034029
IF-MIB::ifOutOctets.40 = Counter32: 696530557
IF-MIB::ifOutOctets.41 = Counter32: 3566533307
IF-MIB::ifOutOctets.42 = Counter32: 292887072
IF-MIB::ifOutOctets.43 = Counter32: 2635635719
IF-MIB::ifOutOctets.44 = Counter32: 717571256
IF-MIB::ifOutOctets.45 = Counter32: 703805109
IF-MIB::ifOutOctets.46 = Counter32: 0
IF-MIB::ifOutOctets.47 = Counter32: 0
IF-MIB::ifOutOctets.48 = Counter32: 4159082059
IF-MIB::ifOutOctets.1000 = Counter32: 0
IF-MIB::ifOutOctets.1001 = Counter32: 0
IF-MIB::ifOutOctets.1002 = Counter32: 0
IF-MIB::ifOutOctets.1003 = Counter32: 0
IF-MIB::ifOutOctets.1004 = Counter32: 0
IF-MIB::ifOutOctets.1005 = Counter32: 0
IF-MIB::ifOutOctets.1006 = Counter32: 0
IF-MIB::ifOutOctets.1007 = Counter32: 0
IF-MIB::ifOutOctets.100000 = Counter32: 0

Open in new window




IF-MIB::ifInOctets.1 = Counter32: 0
IF-MIB::ifInOctets.2 = Counter32: 3584404
IF-MIB::ifInOctets.3 = Counter32: 2032285530
IF-MIB::ifInOctets.4 = Counter32: 3147193
IF-MIB::ifInOctets.5 = Counter32: 2966294211
IF-MIB::ifInOctets.6 = Counter32: 198608187
IF-MIB::ifInOctets.7 = Counter32: 1376848509
IF-MIB::ifInOctets.8 = Counter32: 2820608503
IF-MIB::ifInOctets.9 = Counter32: 112169
IF-MIB::ifInOctets.10 = Counter32: 0
IF-MIB::ifInOctets.11 = Counter32: 2003352404
IF-MIB::ifInOctets.12 = Counter32: 3212304972
IF-MIB::ifInOctets.13 = Counter32: 1733439655
IF-MIB::ifInOctets.14 = Counter32: 0
IF-MIB::ifInOctets.15 = Counter32: 3448924058
IF-MIB::ifInOctets.16 = Counter32: 0
IF-MIB::ifInOctets.17 = Counter32: 597548551
IF-MIB::ifInOctets.18 = Counter32: 2457007937
IF-MIB::ifInOctets.19 = Counter32: 1418708261
IF-MIB::ifInOctets.20 = Counter32: 1427392
IF-MIB::ifInOctets.21 = Counter32: 2393885805
IF-MIB::ifInOctets.22 = Counter32: 3871715060
IF-MIB::ifInOctets.23 = Counter32: 3351239553
IF-MIB::ifInOctets.24 = Counter32: 3326691083
IF-MIB::ifInOctets.25 = Counter32: 3493132
IF-MIB::ifInOctets.26 = Counter32: 654694375
IF-MIB::ifInOctets.27 = Counter32: 7101
IF-MIB::ifInOctets.28 = Counter32: 9397
IF-MIB::ifInOctets.29 = Counter32: 0
IF-MIB::ifInOctets.30 = Counter32: 193636
IF-MIB::ifInOctets.31 = Counter32: 3496355246
IF-MIB::ifInOctets.32 = Counter32: 1097960460
IF-MIB::ifInOctets.33 = Counter32: 191396222
IF-MIB::ifInOctets.34 = Counter32: 0
IF-MIB::ifInOctets.35 = Counter32: 3698897483
IF-MIB::ifInOctets.36 = Counter32: 990087774
IF-MIB::ifInOctets.37 = Counter32: 1833219
IF-MIB::ifInOctets.38 = Counter32: 4413742
IF-MIB::ifInOctets.39 = Counter32: 2384945
IF-MIB::ifInOctets.40 = Counter32: 2679730
IF-MIB::ifInOctets.41 = Counter32: 3611231156
IF-MIB::ifInOctets.42 = Counter32: 3607469970
IF-MIB::ifInOctets.43 = Counter32: 1737840972
IF-MIB::ifInOctets.44 = Counter32: 36287190
IF-MIB::ifInOctets.45 = Counter32: 448738295
IF-MIB::ifInOctets.46 = Counter32: 0
IF-MIB::ifInOctets.47 = Counter32: 0
IF-MIB::ifInOctets.48 = Counter32: 1059962400
IF-MIB::ifInOctets.1000 = Counter32: 0
IF-MIB::ifInOctets.1001 = Counter32: 0
IF-MIB::ifInOctets.1002 = Counter32: 0
IF-MIB::ifInOctets.1003 = Counter32: 0
IF-MIB::ifInOctets.1004 = Counter32: 0
IF-MIB::ifInOctets.1005 = Counter32: 0
IF-MIB::ifInOctets.1006 = Counter32: 0
IF-MIB::ifInOctets.1007 = Counter32: 0
IF-MIB::ifInOctets.100000 = Counter32: 0

Open in new window

0
Comment
Question by:3XLcom
  • 5
  • 3
  • 2
10 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
awk -F: '
  {if($3~/ifOutOctets.2/) print "Upload 2", $4;
   if($3~/ifInOctets.2/) print "Download 2", $4}
  ' a.txt b.txt > c.txt
0
 

Author Comment

by:3XLcom
Comment Utility
what aboot that  IF-MIB::ifInOctets.8 = Counter32: 2820608503
it will get the 2 part for this line 8

ifOutOctets will be output
number will come as same
2820608503 this part will be the 3rd word on line
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
Comment Utility
Ok,

I assumed that you want to collect the ".2" data exclusively. I indeed made a mistake by not ruling out the .20, .21 etc. values, sorry. It has nothing to do with the ".8" line which you quoted, however.

The mistake can easily be corrected:

awk -F: '
  {if($3~/ifOutOctets.2 /) print "Upload 2", $4;
   if($3~/ifInOctets.2 /) print "Download 2", $4}
  ' a.txt b.txt

Or do you want to get all the values .1 - .100000 with the appropriate number in the third column? If so, please let me know!
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
Comment Utility
awk -F'[ .]' '/Octets.2 /{print (/In/?"Up":"Down") "load ",$2,$5}' b.txt a.txt > c.txt
0
 

Author Comment

by:3XLcom
Comment Utility
[root@sflow snmpactivity]# awk -F'[ .]' '/Octets.2 /{print (/In/?"Up":"Down") "load ",$2,$5}'  TO1.OUT.Sat-Dec-28-09-32-58-2013.txt TO1.IN.Sat-Dec-28-09-32-58-2013.txt > R.txt
awk: (FILENAME=TO1.OUT.Sat-Dec-28-09-32-58-2013.txt FNR=57) fatal: cannot open file `TO1.IN.Sat-Dec-28-09-32-58-2013.txt' for reading (No such file or directory)
[root@sflow snmpactivity]# ls -l
total 40
-rw-r--r-- 1 root root 2429 Dec 28 09:33 K16.IN.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root 2532 Dec 28 09:33 K16.OUT.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root 2296 Dec 28 09:33 K17.IN.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root 2399 Dec 28 09:32 K17.OUT.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root 2652 Dec 28 09:33 ROUTER.IN.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root 2721 Dec 28 09:33 ROUTER.OUT.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root   22 Dec 28 09:38 R.txt
-rw-r--r-- 1 root root   78 Dec 28 09:30 snmpactivity.sh
-rw-r--r-- 1 root root 2383 Dec 28 09:33 T01.IN.Sat-Dec-28-09-32-58-2013.txt
-rw-r--r-- 1 root root 2475 Dec 28 09:33 TO1.OUT.Sat-Dec-28-09-32-58-2013.txt

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:3XLcom
Comment Utility
woolmilk we want to read 1 to 100.000 or more some snmp resources give uninnormal numbers and we want to read all


[root@sflow snmpactivity]# awk -F: ' {if($3~/ifOutOctets.2 /) print "Upload 2", $4;  if($3~/ifInOctets.2 /) print "Download 2", $4}'  K16.IN.Sat-Dec-28-09-32-58-2013.txt  K16.OUT.Sat-Dec-28-09-32-58-2013.txt
Download 2  7108816
Upload 2  1039724573

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
We produce the requested c.txt given the given the provided examples of a.txt and b.txt
If we do not provide the desired result when given different input files, we can't know that unless you show us input files for which it does not give the result you want, and you tell us the result that you would want in that case.
0
 

Author Comment

by:3XLcom
Comment Utility
no it is the same file i have done like this you should try with the given output above so please just help me to order this with 3 of columns


Port No  Download  Upload
1                0                0
2                7108816    1039724573

.....

sth. like this


I do not know any thing awk but i am learning :) sorry



[root@sflow snmpactivity]# awk -F: '{if($3~/^ifOutOctets/) print substr($3,13,index($3," ")-13),  "Upload ", $4; if($3~/^ifInOctets/) print substr($3,12,index($3," ")-12),  "Download ", $4; }'  K16.IN.Sat-Dec-28-09-32-58-2013.txt  K16.OUT.Sat-Dec-28-09-32-58-2013.txt
1 Download   0
2 Download   7108816
3 Download   654178394
4 Download   6708697
5 Download   3879712434
6 Download   495052180
7 Download   1091449111
8 Download   2486272906
9 Download   184820
10 Download   0
11 Download   4175303965
12 Download   3427907676
13 Download   2666248020
14 Download   0
15 Download   1703539735
16 Download   0
17 Download   1329470598
18 Download   2938815786
19 Download   1926992239
20 Download   2849539
21 Download   964573591
22 Download   1864520824
23 Download   3533326166
24 Download   3994536250
25 Download   7399890
26 Download   2619944596
27 Download   12018
28 Download   19130
29 Download   0
30 Download   322350
31 Download   2745043008
32 Download   1394934553
33 Download   3618359215
34 Download   0
35 Download   972194499
36 Download   2182129261
37 Download   3134719
38 Download   7659957
39 Download   5978462
40 Download   5820174
41 Download   708267774
42 Download   2340552635
43 Download   830665118
44 Download   66180399
45 Download   514069968
46 Download   0
47 Download   0
48 Download   3709374541
1000 Download   0
1001 Download   0
1002 Download   0
1003 Download   0
1004 Download   0
1005 Download   0
1006 Download   0
1007 Download   0
100000 Download   0
1 Upload   0
2 Upload   1039724573
3 Upload   544668070
4 Upload   1039471988
5 Upload   2752690738
6 Upload   2361908194
7 Upload   2952394144
8 Upload   3804707238
9 Upload   1035426001
10 Upload   0
11 Upload   3040617203
12 Upload   4091442520
13 Upload   2854414067
14 Upload   0
15 Upload   3190039200
16 Upload   0
17 Upload   2311914785
18 Upload   240470595
19 Upload   1593489718
20 Upload   1033778972
21 Upload   3587910203
22 Upload   1732175453
23 Upload   1534076014
24 Upload   1538468183
25 Upload   1039814339
26 Upload   375109529
27 Upload   1035297324
28 Upload   1035307898
29 Upload   0
30 Upload   1035481688
31 Upload   1102165777
32 Upload   1703461915
33 Upload   3820172448
34 Upload   0
35 Upload   2174546577
36 Upload   4202713244
37 Upload   1035456381
38 Upload   1040234513
39 Upload   1038808210
40 Upload   1039043902
41 Upload   2711300616
42 Upload   967576214
43 Upload   120440745
44 Upload   1076678063
45 Upload   1042832992
46 Upload   0
47 Upload   0
48 Upload   2400730543
1000 Upload   0
1001 Upload   0
1002 Upload   0
1003 Upload   0
1004 Upload   0
1005 Upload   0
1006 Upload   0
1007 Upload   0
100000 Upload   0

Open in new window

0
 

Author Closing Comment

by:3XLcom
Comment Utility
thnaks i have done with your help
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I'm not sure which of the examples in your last post are a.txt b.txt or c.txt, but maybe you want something like
awk -F'[ .]' '/Out/{c[$2]=c[$2] "\t" $5}/In/{c[$2]=$5 c[$2]}END{OFS="\t";print "Port No\tDownload\tUpload";for( p in c){print p,c[p]}}' a.txt b.txt | sort -n > c.txt
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now