Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing.
Desired output is to have the script replace "foo" with "bar" in field 2 of FILENAME only on lines beginning with Username entered into read.
After this step the idea is to replace "foo" with ANY data that's in the specified Field, and the Field number to also be specified by the user via a read command.
So if anyone knows a simpler command for doing that all in 1 go that'd be much appreciated too.
Thanks for the help but that doesn't produce any output for me.
do you not have to -v and assign the shell variable as an awk variable before using it with an awk command?
---------- Post updated at 06:25 AM ---------- Previous update was at 06:21 AM ----------
I just created the exact same files you have used and it works perfectly, yet there's no difference i can see in my practical file...
two seconds and i'll run some more tests...
EDIT:
This will teach me for trying to write scripts after being awake for 24 hours.
My shell variable was named $UserName
the variable i was trying to use was name $Username
simple idiotic mistake.
Thanks for your help, your last post really made me go back and look at other things it could be once i knew the syntax was already correct.
---------- Post updated at 06:31 AM ---------- Previous update was at 06:25 AM ----------
Moving on to your other request. What you posted should also work. But it would replace *all* occurrances of data with newdata in that record. Using sub will replace the first occurance of that data with newdata in that record.
read data
read newdata
awk -F: -v var=${Username} -v var1=${data} -v var2=${newdata} '$0 ~ var {gsub(var1,var2); print}' FILENAME
+ Username=Richard
+ awk -F: '/^Richard:/ {gsub(/foo/,"bar", $2); print}' FILENAME
Richard bar
+ awk -F: -v var=Richard '$0 ~ var {gsub(/foo/,"bar",$2); print}' FILENAME
Richard bar
Bug bar Richard
There is extra line which doesn't match the requirement with the second awk oneliner.
You have to indeed. My point is you might just don't need an awk variable in the first place, especially if you use it only once.