awk modification for lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk modification for lines
# 1  
Old 03-14-2013
awk modification for lines

so i have this data in a file:

Code:
jime=1860,yime=1.23243,lime=[Mar-12-(UTC)]
jime=1859,yime=1.23018,lime=[Mar-12-(UTC)]
jime=1825,yime=1.15371,lime=[Mar-12-(UTC)]
jime=1849,yime=1.20769,lime=[Mar-12-(UTC)]
jime=1841,yime=1.1897,lime=[Mar-12-(UTC)]
jime=1849,yime=1.20769,lime=[Mar-12-(UTC)]

i use this code to calculate the percentage difference of the number in column 2 (comma delimited):

Code:
awk -F"[=,]" '{prev=$4; print; getline; print $0","($4-prev)/(prev<0?-prev:prev)*100"%"}'

currently, the above code calculates the difference like this:
Code:
jime=1860,yime=1.23243,lime=[Mar-12-(UTC)]
and
jime=1859,yime=1.23018,lime=[Mar-12-(UTC)]

then

Code:
jime=1825,yime=1.15371,lime=[Mar-12-(UTC)]
and
jime=1849,yime=1.20769,lime=[Mar-12-(UTC)]

and so on

i need to alter the awk statement so it instead calculates the percentage difference line by line. meaning, from one line to the next. not every other line. is this possible?

Last edited by SkySmart; 03-14-2013 at 01:07 PM..
# 2  
Old 03-14-2013
Is this what you are looking for?
Code:
awk -F'[=,]' ' {
                V[++n] = $4
                R[n] = $0
} END {
                for ( i = 1; i <= n; i++ )
                        print R[i], ( V[i] - V[i+1] ) / V[i] * 100
} ' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-14-2013
Quote:
Originally Posted by bipinajith
Is this what you are looking for?
Code:
awk -F'[=,]' ' {
                V[++n] = $4
                R[n] = $0
} END {
                for ( i = 1; i <= n; i++ )
                        print R[i], ( V[i] - V[i+1] ) / V[i] * 100
} ' OFS=, file


i ran your command and got this:

Code:
#### awk -F'[=,]' ' { V[++n] = $4 R[n] = $0 } END { for ( i = 1; i <= n; i++ ) print R[i], ( V[i] - V[i+1] ) / V[i] * 100 } ' OFS=, file.txt
awk:  { V[++n] = $4 R[n] = $0 } END { for ( i = 1; i <= n; i++ ) print R[i], ( V[i] - V[i+1] ) / V[i] * 100 } 
awk:                     ^ syntax error
####

# 4  
Old 03-14-2013
If you want to run it in single line, then you have to use semi-colon ; to separate statements:
Code:
awk -F'[=,]' '{V[++n]=$4;R[n]=$0}END{for(i=1;i<=n;i++) print R[i],(V[i]-V[i+1])/V[i]*100}' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 03-14-2013
Quote:
Originally Posted by bipinajith
If you want to run it in single line, then you have to use semi-colon ; to separate statements:
Code:
awk -F'[=,]' '{V[++n]=$4;R[n]=$0}END{for(i=1;i<=n;i++) print R[i],(V[i]-V[i+1])/V[i]*100}' OFS=, file

thank you so much!!!!

---------- Post updated at 02:28 PM ---------- Previous update was at 12:47 PM ----------

sorry to bring this up again.

Code:
jime=8,yime=1.23243,lime=[Mar-12-(UTC)]
jime=1,yime=1.23018,lime=[Mar-12-(UTC)]
jime=8,yime=1.15371,lime=[Mar-12-(UTC)]

what if i want to do the percentage difference on the numbers in the first column? and then tack it to the results?

like this:


Code:
jime=8,yime=1.23243,lime=[Mar-12-(UTC)],474,803
jime=1,yime=1.23018,lime=[Mar-12-(UTC)],893,1600
jime=8,yime=1.15371,lime=[Mar-12-(UTC)],200,400


i tried:

Code:
awk -F'[=,]' '{V[++n]=$2;R[n]=$0}END{for(i=1;i<=n;i++) print R[i],( V[i+1] - V[i] ) / (V[i+1]>0?-V[i+1]:V[i+1]?V[i+1]<0?V[i+1]:-V[i+1]) *100}' OFS=,

but i'm pretty sure i mangled it and its incorrect. the problem i'm having with the numbers is, take a look at this:

Code:
8
1
8

the above number was at 8, then it went down to 1, then it went back up to 8.

so the results should be:

Code:
8,-700
1,700
8

notice the negative sign.

when i run this:
Code:
awk -F'[=,]' '{V[++n]=$2;R[n]=$0}END{for(i=1;i<=n;i++) print R[i],( V[i+1] - V[i] ) / (V[i+1]>0?-V[i+1]:V[i+1]) *100}'

i get:

Code:
8,-700
1,87.5
8

# 6  
Old 03-14-2013
You have to change the divisor accordingly depending on if value is increasing or decreasing to get expected result:
Code:
awk -F'[=,]' ' {
        V[++n] = $2
        R[n]  = $0
} END {
        for ( i = 1; i <= n; i++) {
                if(V[i+1] > 0 && V[i+1] > V[i])
                        print R[i], ((V[i+1] - V[i]) / V[i]) * 100
                else if (V[i+1] > 0 && V[i+1] < V[i])
                        print R[i], ((V[i+1] - V[i]) / V[i+1]) * 100
                else
                        print R[i]
        }
} ' OFS=, file

# 7  
Old 03-14-2013
Quote:
Originally Posted by bipinajith
You have to change the divisor accordingly depending on if value is increasing or decreasing to get expected result:
Code:
awk -F'[=,]' ' {
        V[++n] = $2
        R[n]  = $0
} END {
        for ( i = 1; i <= n; i++) {
                if(V[i+1] > 0 && V[i+1] > V[i])
                        print R[i], ((V[i+1] - V[i]) / V[i]) * 100
                else if (V[i+1] > 0 && V[i+1] < V[i])
                        print R[i], ((V[i+1] - V[i]) / V[i+1]) * 100
                else
                        print R[i]
        }
} ' OFS=, file


this looks like it'll work perfect. however, the numbers i'm getting confuses me:

datafile:
Code:
jime=8,yime=1.23243,lime=[Mar-12-(UTC)],lap=985434334
jime=1,yime=1.23018,lime=[Mar-12-(UTC)],lap=938354343
jime=8,yime=1.15371,lime=[Mar-12-(UTC)],lap=482323232
jime=13,yime=1.00371,lime=[Mar-12-(UTC)],lap=482323232
jime=7,yime=1.31371,lime=[Mar-12-(UTC)],lap=482323232
jime=2,yime=1.09371,lime=[Mar-12-(UTC)],lap=482323232

after running the command on this datafile, i get:
Code:
jime=8,yime=1.23243,lime=[Mar-12-(UTC)],lap=985434334,-700
jime=1,yime=1.23018,lime=[Mar-12-(UTC)],lap=938354343,700
jime=8,yime=1.15371,lime=[Mar-12-(UTC)],lap=482323232,62.5
jime=13,yime=1.00371,lime=[Mar-12-(UTC)],lap=482323232,-85.7143
jime=7,yime=1.31371,lime=[Mar-12-(UTC)],lap=482323232,-250
jime=2,yime=1.09371,lime=[Mar-12-(UTC)],lap=482323232

the numbers i highlighted in red are not correct.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification to awk command

i have a php file that has this: php.code #!/usr/bin/php <?php phpinfo(); hlight_file(__FILE__); ?> I want my awk code grab whatever is inbetween and including the "<?php" and "?>". Then, it should scan all the entries between these two points. And if the entries between these... (10 Replies)
Discussion started by: SkySmart
10 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. Shell Programming and Scripting

awk script modification

can someone help me identify what i'm doing wrong here: awk -F'|' 'BEGIN{c=0} /./ && /./ { if ($3 < 2) { print ; c++ } END { print c":OK" } else if (($3 >= 2) && ($3 < 4)) { print ; c++ } END { print c":WARNING" } else if ($3 >= 4) { print ; c++ } END { print c":CRITICAL" } }'... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

awk script modification - treat certain files differently

awk 'BEGIN{OFS=","} FNR == 1 {if (NR > 1) {print fn,fnr,nl} fn=FILENAME; fnr = 1; nl = 0} {fnr = FNR} /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++} <'{system ("gunzip -cd FILENAME")}' END ... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

6. UNIX for Dummies Questions & Answers

awk output modification

Hello, I am using awk command to print some output, but there are some characters that I would like to remove from the output awk '{print $5$6}' the output I get is column5/:column6 I am looking forward to remove the : and to get the output column5/column6 Sorry if this question is... (4 Replies)
Discussion started by: Error404
4 Replies

7. Shell Programming and Scripting

awk script modification

I want the below script to omit every chunk of data that contains a specific hostname. here's the scenario. i have a configuration file that contains the configuration of several hosts. a sample of this configuration file is this: define host { address ... (12 Replies)
Discussion started by: SkySmart
12 Replies

8. Shell Programming and Scripting

Awk modification

I need help modifying the code below. DATAFILE is a log file. I have two strings i need to search for in the log file. The two strings are: 1. ERROR 2. com.rolander.promotions.client awk 'BEGIN { while((getline < "'${SFILE}'")>0) S FS="\n"; RS="\n" } (11 Replies)
Discussion started by: SkySmart
11 Replies

9. Shell Programming and Scripting

in line modification in a file using awk

Hi, I have a conf.file with the following values: ef=78 b=40 ca=40 c=45/dev2 when I modify one of the line with the below awk script,it's modifying BUT it's placing the modified line in the last line : input:- Configure b 45/dev4 output:- ef=78 ca=40 ... (2 Replies)
Discussion started by: axes
2 Replies

10. Shell Programming and Scripting

Operations on a file with Deletion , Modification and Insertion of lines

Hi All , Given a file , I need to delete , modify and insert lines matching certain patterns in that file using shell scripting. e.g. If a file FILE1 has following content : CUST.ABC.DEF = CUST.ABC.DEF * CUST.ABC.DEF PRINTF(CUST.ABC.DEF) CUST.ABC.DEF = mid(CUST.ABC.DEF,10.34)... (5 Replies)
Discussion started by: swapnil.nawale
5 Replies
Login or Register to Ask a Question