Weird Interpretation by Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird Interpretation by Awk
# 1  
Old 10-14-2008
Weird Interpretation by Awk

Hi,

I am not sure what I am doing wrong but I am messing up some logic here. The input file is something like this:

Code:
*___String Type A Here___String Type B Here___123
*___                         ___String Type B Here___123
*___                         ___String Type B Here___123
*___                         ___String Type B Here___123
*___                         ___String Type B Here___123
*___String Type A Here___String Type B Here___123
*___                         ___String Type B Here___123
*___                         ___String Type B Here___123
*___                         ___String Type B Here___123
*___                         ___String Type B Here___123

When I write something like

Code:
 awk '{print $2}' filename

For some reason, awk prints the first line correctly, but the second line gets printed with the Type B string which I do not want... I want that to be blank... Could someone please tell me if there is a workaround for this?
# 2  
Old 10-14-2008
what's your expected result, and your original input file?
# 3  
Old 10-14-2008
By default awk uses whitespace(s) [tab and space ] as the field separator.
Any number of spaces/tabs still make just one field separator. You cannot get a blank field in awk, without changing the FS variable.

Beyond that, I don't know what you are trying to do....
# 4  
Old 10-14-2008
Seconding freelong's request. Awk is heavily dependent on field separators, and so any analysis will require some knowledge of the structure of the input file. Can you post a few actual lines from it?
# 5  
Old 10-14-2008
Yes sure... I was trying to process a BGP Dump file. I am not sure if I can get the exact structure here though.

Code:
*  3.0.0.0          193.0.0.56                             0 3333 3356 701 703 80 i
*                   217.75.96.60             0             0 16150 3549 701 703 80 i
*                   64.125.0.137           124             0 6461 701 703 80 i
*                   209.10.12.156            0             0 4513 701 701 703 80 i
*                   134.55.200.1                           0 293 701 703 80 i
*                   194.85.4.55                            0 3277 3216 3549 701 703 80 i
*                   66.59.190.221                          0 6539 2914 701 703 80 i
*                   209.10.12.160                          0 4513 701 701 703 80 i
*                   164.128.32.11                          0 3303 701 703 80 i
*                   207.46.32.34                           0 8075 701 703 80 i
*                   196.7.106.245            0             0 2905 702 703 80 i
*                   154.11.11.113            0             0 852 1239 701 703 80 i
*                   89.149.178.10           10             0 3257 701 703 80 i
*                   154.11.98.225            0             0 852 1239 701 703 80 i
*>                  157.130.10.233                         0 701 703 80 i
*                   129.250.0.171            0             0 2914 701 703 80 i
*                   129.250.0.11             6             0 2914 701 703 80 i
*                   66.185.128.48          504             0 1668 701 703 80 i
*                   4.68.1.166               0             0 3356 701 703 80 i
*                   207.172.6.20             5             0 6079 3356 701 703 80 i
*                   213.140.32.146                         0 12956 3356 701 703 80 i
*                   65.106.7.139             3             0 2828 701 703 80 i
*                   207.246.129.13                         0 11608 2914 701 703 80 i
*                   203.62.252.186                         0 1221 4637 703 80 i
*                   193.251.245.6                          0 5511 701 703 80 i
*                   195.219.96.239                         0 6453 701 703 80 i
*                   207.45.223.244                         0 6453 701 703 80 i
*                   12.0.1.63                              0 7018 701 703 80 i
*                   144.228.241.81  4294967294             0 1239 701 703 80 i
*                   208.51.134.254           0             0 3549 701 703 80 i
*                   206.24.210.100                         0 3561 701 703 80 i
*                   202.232.0.2                            0 2497 701 703 80 i
*                   134.222.85.45                          0 286 3549 701 703 80 i
*                   207.172.6.162            6             0 6079 3356 701 703 80 i
*                   209.10.12.125         4122             0 4513 701 701 703 80 i
*                   216.218.252.164                        0 6939 3549 701 703 80 i
*                   203.181.248.168                        0 7660 2516 209 701 703 80 i
*                   202.249.2.86                           0 7500 2497 701 703 80 i
*                   64.71.255.61                           0 812 6461 701 703 80 i
*  4.0.0.0/9        193.0.0.56                             0 3333 3356 i
*                   217.75.96.60             0             0 16150 1239 3356 i
*                   64.125.0.137           124             0 6461 3356 i
*                   209.10.12.156            0             0 4513 3356 3356 3356 i
*                   134.55.200.1                           0 293 3356 i
*                   194.85.4.55                            0 3277 3267 25462 3356 i
*                   66.59.190.221                          0 6539 2914 3356 i
*                   209.10.12.160                          0 4513 701 701 3356 i

As you can observe, I want only the IP Addresses in the second column. What I actually want to do is to replicate the same IP address until the next IP address is encountered. So the expected output is something like this:

Code:
3.0.0.0    3333 3356 701 703 80
3.0.0.0    16150 3549 701 703 80
... so on... until the next IP is encountered
4.0.0.0/9    3333 3356
4.0.0.0/9    3561 701 703 80

Hope I was able to convey my requirement properly. Thanks a lot for the advices though.
# 6  
Old 10-14-2008
As the earlier posters intimated, the varying number of fields means you can't rely on field numbers... but since the output is neatly formatted and the fields are at constant character offsets you can use them instead:

Code:
awk '
        {
                tempnetwork=substr($0,4,15)
                if (tempnetwork !~ /^ *$/) network=tempnetwork
                nums=substr($0,61)
                print network,nums
        }
' inputfile > outputfile

# 7  
Old 10-14-2008
The character offset idea is excellent. You might also find the idea of "x from last" useful-- that is, instead of printing fields 5, 6 and 7, you can print the third from last, second from last, and last fields. The structure for this is:

Code:
 awk '{print $(NF-2) " " $(NF-1) " " $NF}'

Best of luck.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk weird error

Here is the awk code i wrote : if ; then gawk -v field_position="$field_position" -v field_length="$field_length" -v header="$header" -v trailer="$trailer" -v lr="$lr" '{ if(NR==1&&header=="1") { next } if(NR==lr&&trailer=="1") { next }... (1 Reply)
Discussion started by: ysvsr1
1 Replies

2. Shell Programming and Scripting

Weird awk problem

Hi, I have a simple awk script: BEGIN{} { $a=$2-$1; print $a } END{if(NR==0){ print "0" } } to which I provide the following input 2.9 14 22.2 27 (4 Replies)
Discussion started by: jamie_123
4 Replies

3. Shell Programming and Scripting

Interpretation of awk code in linux

HI I have a following code: CM_PROJHOME_DIR=`echo ${SCRNAME} | awk '{FS="/"; p=NF-5; for (i=1; i<= p ; i++) t=t$i"/"; print t}'` where SCRNAME=`whence $0 | sed -e 's/\.\///g'` I need to modify the code to work in linux and also the interpretation like what is the code exactly doing.... (4 Replies)
Discussion started by: vee_789
4 Replies

4. Shell Programming and Scripting

awk weird problem.

awk 'BEGIN{print 1.2.3.4}' 1.20.30.4 Can anyone explain why has extra "0" in the IP address? (3 Replies)
Discussion started by: newoz
3 Replies

5. Shell Programming and Scripting

awk print behavior weird

Hi Experts I am facing a weird issue while using print statement in awk. I have a text file with 3 fields shown below: # cat f1 234,abc,1000 235,efg,2000 236,jih,3000 # When I print the third column alone, I dont face any issue as shown below: # awk '{print $3 }' FS=, f1 1000 2000... (5 Replies)
Discussion started by: guruprasadpr
5 Replies

6. Shell Programming and Scripting

awk, sed, grep...weird style

my desired output is like this: so the thing is, I only need to show every of this part out but the frequency of that data is not fixed, so sometimes it may have 4 lines, or 6 lines or whatever in that file. However, the last line will always have empty space/line below it. (13 Replies)
Discussion started by: finalight
13 Replies

7. UNIX for Advanced & Expert Users

Weird Awk issue

Hi All, a bit of a weird one here. I'm trying to pass a variable into an awk command, and I keep getting an error. I have the line nawk -F"," -v red=$random_variable '{print $red}' $w_dir/$file_name > $w_dir/${column_name} that keeps failing with the error nawk: can't open file {print... (17 Replies)
Discussion started by: Khoomfire
17 Replies

8. UNIX for Advanced & Expert Users

Weird scenario with Awk

Guys, this one is rather odd. I've got an array of numbers, and I'm trying to select only the records with the string "Random" in the 4th column. I'm using awk in this format: awk '{ if (( $6 -eq Random )) print $0 }' For some odd reason, this is simply giving me the list of all the entries... (4 Replies)
Discussion started by: Khoomfire
4 Replies

9. AIX

interpretation of sar

hello with a sar i have this result: System configuration: lcpu=48 ent=4.00 14:06:37 %usr %sys %wio %idle physc %entc 14:06:39 26 9 3 62 1.63 40.7 14:06:41 26 9 3 63 1.58 39.4 14:06:43 ... (0 Replies)
Discussion started by: pascalbout
0 Replies

10. UNIX for Advanced & Expert Users

SAR -b interpretation

I have used SAR -b to get some Unix cache / buffer metrics and the results are confusing me a bit. The pread/s & pwrit/s are showing 0. However the lread/s and lwrit/s are showing figures. I note also that the bread/s and bwrit/s are showing figures. I believe that pread/s and pwrit/s is not... (3 Replies)
Discussion started by: jimthompson
3 Replies
Login or Register to Ask a Question