Reputation: 320
I want to find & replace values in column 5 & 6 based on condition given to column 1. If first column has value 2159 then it should replace value in column 5 by 13.49694 if it has value 13.512034 and replace value in column 6 by 78.22772 if it has value 78.226233. I tried with following command but it replacing all occurrences of 78.226233 by 78.22772 in column 6. What I want is it should replace only for 2159 value in column 1.
awk -F ',' -v OFS=',' '$1 ~ /^2159/ && $5=="13.512034"{$5="13.49694"}1 && $6=="78.226233"{$6="78.22772"}1' 14update1.csv > 14update2.csv
Is there any way to update the change in same file? I am pretty new to awk and shell scripting so I apologize if this is an easy fix.
The datafile I have is something like:
2159,KOLAR,SRINIVASAPURA,GAUNIPALLI (KODIPALLI) (GP),13.49694,78.22772,14-08-17,22,0
2159,KOLAR,SRINIVASAPURA,GAUNIPALLI (KODIPALLI) (GP),13.49694,78.22772,14-08-17,23,0
2159,KOLAR,SRINIVASAPURA,GAUNIPALLI (KODIPALLI) (GP),13.49694,78.22772,14-08-17,23,0
2159,KOLAR,SRINIVASAPURA,GAUNIPALLI (KODIPALLI) (GP),13.49694,78.22772,14-08-17,23,0
2159,KOLAR,SRINIVASAPURA,GAUNIPALLI (KODIPALLI) (GP),13.49694,78.22772,14-08-17,23,0
3358,KOLAR,SRINIVASAPURA,Gownipalli (GP),13.512034,78.22772,14-08-17,0,0
3358,KOLAR,SRINIVASAPURA,Gownipalli (GP),13.512034,78.22772,14-08-17,0,0
3358,KOLAR,SRINIVASAPURA,Gownipalli (GP),13.512034,78.22772,14-08-17,0,0
3358,KOLAR,SRINIVASAPURA,Gownipalli (GP),13.512034,78.22772,14-08-17,0,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,0,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,0,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,0,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,0,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,1,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,1,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,1,0
3317,KOLAR,SRINIVASAPURA,Hodali (GP),13.358552,78.269189,14-08-17,1,0
Upvotes: 0
Views: 651
Reputation: 37394
Using sub()
:
$ awk '
BEGIN {
FS=OFS=","
}
$1==2159 { # only one condition
sub(/^13\.512034$/,"13.49694",$5) # or if($5=="13.512034") $5="13.49694"
sub(/^78\.226233$/,"78.22772",$6) # ditto
}
1' file
Output:
2159,23,45,45,13.49694,78.22772
Upvotes: 1
Reputation: 41446
This awk
should do:
cat file
2159,23,45,45,13.512034,78.226233
awk -F, -v OFS="," '$1==2159 && $5==13.512034 {$5="13.49694"} $1==2159 && $6==78.226233 {$6="78.22772"} 1' file
2159,23,45,45,13.49694,78.22772
This $1 ~ /^2159/
does starts with, not equal to. $1=2159
or $1~/^2159$/
Upvotes: 2