grep + awk

hi,

i want to replace $3 in file by $var i have used this line of command :

awk '!$3{$3=v}1' OFS="," FS="," v="$var" $file1> $file2

but i want just to replace it in specific line where when i found $string

i used this command :

grep $string $file1 | awk '!$3{$3=v}1' OFS="," FS="," v="$var" $file1> $file2

but i have the $3 changed in all the file1

any help please.

thanks

That's the consequence if you just copy code from others without understanding how it works. You really should read an awk manual first.

BTW the input(s) of your awk command isn't proper.

Regards

thanks for your replay but i know that this line of code is your answer for another question but the problem for me is not awk cause i just want to finish this script which is about 50 lines so i can't optimize my code for the time .

:slight_smile:

awk -v string=$string -v var=$var '{ if($0 ~ string) $3=var; print }' filename