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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating data in text file gets unwanted data

oldfart007

Technical User
May 8, 2025
1
working on a script to show internet data used by my router. The snmp data is placed in a text file every day once an hour.
I can't get rid of the first line 372.963 or the last line -407.11 . These are totally messing up my display

here is one of the data files 08-May-25-wrls0ta.txt
372963176
375429610
377839519
380349356
382724138
385122436
392181488
397911741
400312606
404918306
407109524

awk 'FNR==NR { print ($1 - prev)/1000000 } { prev = $1 }' 08-May-25-wrls0ta.txt
The output
372.963
2.46643
2.40991
2.50984
2.37478
2.3983
7.05905
5.73025
2.40087
4.6057
2.19122
-407.11
 
I get this:
Code:
$ awk 'FNR==NR { print ($1 - prev)/1000000 } { prev = $1 }' 08-May-25-wrls0ta.txt 
372.963
2.46643
2.40991
2.50984
2.37478
2.3983
7.05905
5.73025
2.40087
4.6057
2.19122

You get 372.963, because on the first line there is not previous line, so the value prev = 0 and then you get the result
(372963176 - 0) / 1000000 = 372.963176
 
The last line of your input file 08-May-25-wrls0ta.txt is not 407109524, but it is empty line, so $1 = 0, prev = 407109524 and so you get the wrong result ($1 - prev)/1000000 = (0 - 407109524)/1000000 = - 407.109524
Code:
$ awk 'FNR==NR { print ($1 - prev)/1000000 } { prev = $1 }' 08-May-25-wrls0ta.txt
372.963
2.46643
2.40991
2.50984
2.37478
2.3983
7.05905
5.73025
2.40087
4.6057
2.19122
-407.11

So you should decide in your script, how to compute the result on the first line and what to do if the current line is empty
Btw. when your script uses only one input file, then the condition FNR==NR is always true.
 

Part and Inventory Search

Sponsor

Back
Top