recod,amount,noofaccount,count
98,+00000187865779787,00319,000000640/
99,+00000187865779787,00001,000000642/
thsi is my input file my question is
1) Reduce the count on the 98 record by 2
recod,amount,noofaccount,count
98,+00000187865779787,00319,000000640/
99,+00000187865779787,00001,000000642/
thsi is my input file my question is
1) Reduce the count on the 98 record by 2
$nawk -F"," ' /^98/ {for(i=1;i<=NF;i++){if(i==1){printf ("%s",$i-2);} else if (i==NF) {printf(",%s\n",$i);} else {printf(",%s",$i)}}}' input
96,+00000187865779787,00319,000000640/
$cat test
98,+00000187865779787,00319,000000640/
99,+00000187865779787,00001,000000642/
% cat >testfile
98,+00000187865779787,00319,000000640/
99,+00000187865779787,00001,000000642/
app@kf3sv ~/tmp
% awk 'BEGIN {FS=OFS=","} $1==98 {$4=sprintf("%0.9d/", $4-2)}1' testfile
98,+00000187865779787,00319,000000638/
99,+00000187865779787,00001,000000642/
can u please explain detail about script how it is working
---------- Post updated at 04:43 AM ---------- Previous update was at 04:25 AM ----------
can u please explain how the script is working deatail
-F","- Field seperator is ,(comma)
/^98/ - Check if the line starts with 98
for(i=1;i<=NF;i++) - iterate all the fields (comma seperated)
if(i==1){printf ("%s",$i-2);} - if it first field, then subtract the value by 2 (we only taking the rows starts with 98, so it will print 96)
else if (i==NF) {printf(",%s\n",$i);} - if it is last field, then print the field and print new line character
else {printf(",%s",$i)}}}' - for other fields, print ,(comma) and the fileds
how it working please give explain the script
awk 'BEGIN {FS=OFS=","} $1==98 {$4=sprintf("%0.9d/", $4-2)}1' testfile i am new to shell scripting
There are three parts:
1) BEGIN {FS=OFS=","} # make (input) Field Separator and Output Field Separator = ","
then for every line:
2) $1==98 {$4=sprintf("%0.9d/", $4-2)} # if the first field is equal 98, change the fourth field
3) 1 # print every line of input - it's shortcut for 1 {print $0}, where 1 is counted as true value.
$4=sprintf("%0.9d/", $4-2) # apply the string format function with the format "%0.9d/" to the result $4-2 (in awk every variable/field can be treated as a number or a string. If there are some non-digit chars after digit chars they are discarded).