Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing lines using awk 2

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
0
0
GB
Hi,

I would like to make this script more flexible by changing the "1" in "NR==1" into a variable.

In1=`awk 'NR==1' file`
In2=`awk 'NR==3' file`

diff=`expr $In2 - $In1`
echo $diff

The file I need to work on is like this:

1 In1
2 Out1
3 In2
4 Out2
5 In3
6 Out3
7 In4
8 Out4

Basically I need to get the following differences:
In2 - In1
In3 - In2
In4 - In3
Out2 - Out1
Out3 - Out2
Out4 - Out3

Thanks!
 
Hi

The results must be grouped, ins first, then Outs ? If alternating one In, one Out, then again In, aand so on is also good :
Code:
awk 'v[NR%2]{print $2"-"v[NR%2]}{v[NR%2]=$2}' /input/file


Feherke.
 
Hi Feherke,

What do you mean by v[NR%2]? I would like to understand the script you provided, hope you can explain it to me.

And I noticed as well that you have $2 in the script, the computation I need to do is for the actual values which is field 1 ($1).

For the "In values" the output should be
In2 - In1 --> 3 - 1 = 2
In3 - In2 --> 5 - 3 = 2
In4 - In3 --> 7 - 5 = 2

The error I got is this:
awk: syntax error near line 1
awk: bailing out near line 1

Thank you ...

 
Hi,

I saw this in awk's manpage ...

getline var --> reads the next record into var, updates NR and FNR.

is there a way I can use this getline?

Thanks ...

 
Hi

If you want them grouped :
Code:
awk '!(NR%2){v[NR/2]=$2;next}l{print $2"-"l}{l=$2}END{for(i=2;i in v;i++)print v[i]"-"v[i-1]}' /input/file

[gray]# or[/gray]

awk '{v[NR%2,int((NR-1)/2)]=$2}END{for(i=1;i>=0;i--){j=1;while(v[i,j]!="")print v[i,j]"-"v[i,j++-1]}}' /input/file
Note, that I put the operator as plain text : "-". If you like the output, remove the double quotes ( " ).

The remaining question is what other tasks are performed in the shell script.

Feherke.
 
Hi

viadisky said:
What do you mean by v[NR%2]?
If there was a previous even/odd value. The [tt]v[/tt] array holds two values : the last encountered In and Out. The Ins are in the odd, the Outs are in the even lines. And [tt]NR%2[/tt] gives alternating 0s and 1s.
viadisky said:
And I noticed as well that you have $2 in the script, the computation I need to do is for the actual values which is field 1 ($1).
Ah, so the first column in your sample was only the line numbers. Ok, then you have to change the $2 to $1.
viadisky said:
getline var --> reads the next record into var, updates NR and FNR.

is there a way I can use this getline?
Not really. I think there is not place for [tt]getline()[/tt] in this script. You use [tt]getline()[/tt] when need to read the next input line before the execution reaches the end of code and reads the next line automatically. Typically, when you have to pass many values from the parsing of one line to the next. But here is just a few value and the calculation does not involve consecutive lines.

Feherke.
 
Hi Feherke,

Thank you so much for the additional information you provided. I also saw this basic example of NR%2 :

awk 'NR % 2 == 0' file

Looking at the first code you provided (this is still producing errors when I run it). I will try to undertsand it bit-by-bit so i can easily modify it (Hope you will be patient to a beginner like me).

awk 'v[NR%2]{print $2"-"v[NR%2]}{v[NR%2]=$2}'

I understand NR%2 but I'm still confused with the "v" written before it.

Thanks :)




 
Hi

viadisky said:
I understand NR%2 but I'm still confused with the "v" written before it.
The [tt]v[/tt] is an array, and [tt]v[NR%2][/tt] is the reference to the array's 0[sup]th[/sup] [gray]( if even line )[/gray] or 1[sup]st[/sup] [gray]( if odd line )[/gray] element.
Code:
awk 'NR % 2 == 0' file
[gray]# do if is an even line[/gray]

awk 'v[NR%2]{print $2"-"v[NR%2]}{v[NR%2]=$2}'
[gray]# do if is not the first even or first odd line[/gray]
Regarding the first code, my best idea is that... you done something wrong. Works for me with [tt]gawk[/tt] and [tt]mawk[/tt], the [tt]gawk[/tt] both on Linux and CygWin.

Feherke.
 
You wanted this ?
awk 'v[NR%2]{print $2"-"v[NR%2]"-->"$1"-"d"="$1-d}NR%2{v[NR%2]=$2;d=$1}' inputfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Both Feherke's and PHV's codes worked if I use "nawk"...

PHV's script has the actual computation results but it only shows in "In" data ...

ifInOctets_26Jan2007_12:25:00_10_s1/6-ifInOctets_26Jan2007_12:20:00_10_s1/6-->384509644-384506513=3131
ifInOctets_26Jan2007_12:30:01_10_s1/6-ifInOctets_26Jan2007_12:25:00_10_s1/6-->384515007-384509644=5363
ifInOctets_26Jan2007_12:35:01_10_s1/6-ifInOctets_26Jan2007_12:30:01_10_s1/6-->384520622-384515007=5615
ifInOctets_26Jan2007_12:40:00_10_s1/6-ifInOctets_26Jan2007_12:35:01_10_s1/6-->384540431-384520622=19809


I'm trying to modify Feherke's script because his code is showing both "In" and "Out" but from his original script the computations aren't included ...

 
Both Feherke's and PHV's codes worked if I use "nawk"...

PHV's script has the actual computation results but it only shows in "In" data ...

ifInOctets_26Jan2007_12:25:00_10_s1/6-ifInOctets_26Jan2007_12:20:00_10_s1/6-->384509644-384506513=3131
ifInOctets_26Jan2007_12:30:01_10_s1/6-ifInOctets_26Jan2007_12:25:00_10_s1/6-->384515007-384509644=5363
ifInOctets_26Jan2007_12:35:01_10_s1/6-ifInOctets_26Jan2007_12:30:01_10_s1/6-->384520622-384515007=5615
ifInOctets_26Jan2007_12:40:00_10_s1/6-ifInOctets_26Jan2007_12:35:01_10_s1/6-->384540431-384520622=19809


I'm trying to modify Feherke's script because his code is showing both "In" and "Out" but from his original script the computations aren't included ...


Thanks :)
 
I search in google for a free tutorial on this side of awk (putting data in an array) but most tutorial I've seen are showing basic examples ...

I tried to change some portion of the script given by PHV (I find it easier to modify his script because he included the computation already) and I know if I place "!" somewhere in the script I would be able to get the results for the "Out" data and it worked! I don't know how to change this line to show both "In" and "Out" but I'm happy I can get both data if I run the two versions of the script.

nawk 'v[NR%2]{print $2"-"v[NR%2]"-->"$1"-"d"="$1-d}!NR%2{v[NR%2]=$2;d=$1}'

Thanks again ...
 
Hi

viadisky said:
I'm trying to modify Feherke's script because his code is showing both "In" and "Out" but from his original script the computations aren't included ...
Feherke said:
Note, that I put the operator as plain text : "-". If you like the output, remove the double quotes ( " ).
According to your original post :
Code:
[blue]master #[/blue] cat viadisky.txt
1     In1
2     Out1
3     In2
4     Out2
5     In3
6     Out3
7     In4
8     Out4

[blue]master #[/blue] awk 'v[NR%2]{print $2"-"v[NR%2]}{v[NR%2]=$2}' viadisky.txt
In2-In1
Out2-Out1
In3-In2
Out3-Out2
In4-In3
Out4-Out3
According to your second post :
Code:
[blue]master #[/blue] cat viadisky2.txt
1
2
3
4
5
6
7
8

[blue]master #[/blue] awk 'v[NR%2]{print $1" - "v[NR%2]" = "$1-v[NR%2]}{v[NR%2]=$1}' viadisky2.txt
3 - 1 = 2
4 - 2 = 2
5 - 3 = 2
6 - 4 = 2
7 - 5 = 2
8 - 6 = 2
Or I misunderstood you. I am abit confused.

Feherke.
 
OOps, I though you wanted only the In data:
awk 'NR>2{print $2"-"v[NR%2]"-->"$1"-"d[NR%2]"="$1-d[NR%2]}{v[NR%2]=$2;d[NR%2]=$1}' inputfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Both PHV's and Feherke's suggestions are both correct !!! Thanks ...!!!

The only thing I need to do before using the script is that no data is missing because if one "In" data is missing .. the computation will be wrong. The data I need to work on is shown below and as you can see I have missing ifInOctets around 14:10:00. Because of this missing data, the final computation is wrong:


ifInOctets,27Jan2007,14:05:01,Counter: 3774083042
ifOutOctets,27Jan2007,14:05:01,Counter: 1112667388
ifOutOctets,27Jan2007,14:10:00,Counter: 1112668001
ifInOctets,27Jan2007,14:15:00,Counter: 3774104270
ifOutOctets,27Jan2007,14:15:00,Counter: 1112670320

Result using Feherke's script:

1112668001 - 3774083042 = -2661415041
3774104270 - 1112667388 = 2661436882
1112670320 - 1112668001 = 2319


I need to look for a way to ignore the third line in the file and just compute for counter difference in 10 minutes time (between 14:15:00 and 14:05:00).

But at least I arrived with good data for those files without missing entries .... THANKS!!! :)

 
Hi Feherke,

Thanks for the additonal help you provided :)

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top