1

i want to change the value of f3 cell before the echo statement in while loop

Exec &> output.csv  

file = "d:/aa.csv"  

while ifs =','  

read f1 f2 f3 f4 f5 f6 f7 f8  

do  

echo "$f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8"  

done < $file  
3
  • What is ifs? Did you mean to write IFS? Commented Jul 9, 2013 at 19:07
  • Odd that you're asking this. Can't you put in the desired value instead of echoing f3? Commented Jul 9, 2013 at 19:08
  • Not only ifs, spaces around the assignment operator would also cause problems. Commented Jul 9, 2013 at 19:10

4 Answers 4

4

If you want to change the value, then change it. Correcting errors:

while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8  
do  
    f3="something else"
    echo "$f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8"  
done < "$file" > output.csv

or

IFS=,
while read -ra fields; do  
    fields[2]="something else"
    echo "${fields[*]}"
done < "$file" > output.csv
Sign up to request clarification or add additional context in comments.

1 Comment

in your original csv, each column value may be in double quotation "" format. In that case, you can update your f3 with the double quotation "" too. f3="\"something else\""
1

To change f3 to A3 in your csv use this sed command:

sed -i.bak -r 's/^(([^,]+,){2})[^,]+(.+)$/\1A3\3/' aa.csv

Comments

1

With awk:

awk 'BEGIN{FS=OFS=","}{$3="Whatever"}1' input.csv > output.csv

Comments

0

This might work for you (GNU sed):

sed -ri 's/[^,]+/REPLACEMENT/3' file.csv

This changes the third occurence of non-commas to REPLACEMENT.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.