Hi,
I have a CSV file with around 1000 rows (lines) and 1000 columns (fields).
I need to replace mth lines's nth field with a value. (E.g., 400th field of 300th line). how can i do that?
Pl advise.
Thanks
Prvn
Hi,
I have a CSV file with around 1000 rows (lines) and 1000 columns (fields).
I need to replace mth lines's nth field with a value. (E.g., 400th field of 300th line). how can i do that?
Pl advise.
Thanks
Prvn
what have you tried?
I tried extracting the fields other than to be replaced. Using "echo" i could again write to another file with new value.
It worked but looked very inefficient. i guess it can be easily achieved with "awk". Pl advise.
Prvn
show your code.
get the input from the user like lineno and columnno the rest is done by this awk
awk -F"," -v var1=$lineno -v var2=$columnno 'NR == var1{gsub(var2,"value")}{print $0}' filename
Actually you could simply do
awk -F, -v line=$m -v col=$n 'NR == line { $col="value" }1' filename
Thanks Vidhyadhar and era.
Era - when i used your solution, replacement done but "," also vanished.
#cat /tmp/test
1,2,3
4,5,6
7,8,9
#gawk -F, -v line="2" -v col="2" 'NR == line { $col="value" }1' /tmp/test
1,2,3
4 value 6
7,8,9
#
Vidhyadhar - when i ran your solution, o/p remains as acutal file contents (no replacements done).
Thanks
Prvn
Adding -v OFS=, seems to fix that for me, on mawk.
Try adding the OFS to era's solution:
gawk -F, -vl=2 -vc=2 -vOFS=, '(NR==l&&$c="newone")||1' filename
Thanks era and Radoulov, your solution working fine.
#cat /tmp/test
1,2,3
4,5,6
7,8,9
#newone="RPK"
#gawk -F, -vl=2 -vc=2 -vOFS=, -vval=$newone '(NR==l&&$c=val)||1' /tmp/test
1,2,3
4,RPK,6
7,8,9
#
Thanks again!