Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I have the following comma separated file:
line1,45,apple
line2,33,orange
line3,12,lemonI need a script that will replace field 2 with 100. i.e. my expected result is:
line1,100,apple
line2,100,orange
line3,100,lemonI am successful using the awk command {print $1",100,"$3}, but is there a better way? Please advise. Thanks.

Hi:
Awk is a good choice for a tool. You can also change the record separater, set field 2 to 100, and print the whole line:
awk ' BEGIN { FS=","; OFS="," }
{
$2=100
print $0
} ' data.fileNot better, but different.
Regards,
Nails

Hi
something without awk would beIFS=','
{ while read a b c
do
echo "$a,100,$c"
done } < obst.txt
IFS='
'Cheers Frank

Hi:
You can save yourself the trouble of restoring field seperator IFS by doing this:
while IFS="," read a b c
do
echo "$a,100,$c"
done < obst.txtRegards,
Nails

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |