3

I am beginner at bash scripting and I have been trying to fix this for more than 8 hours. I have searched on StackOwerflow and tried the answers to fit my needs, but without success.

I want to use bash script to change csv file's date value to current date.

I am using a dummy .csv file ( http://eforexcel.com/wp/wp-content/uploads/2017/07/100-Sales-Records.zip ) and I want to change the 6th value (date) to the current date.

What I have been doing so far: I have created one line csv to test the script

cat oneline.csv:

Australia and Oceania,Tuvalu,Baby Food,Offline,H,5/28/2010,669165933,6/27/2010,9925,255.28,159.42,2533654.00,1582243.50,951410.50

then I have tested the one line script:

echo `cat oneline.csv | awk -F, '{ print $1"," $2"," $3"," $4"," $5","}'` `date` `cat oneline.csv |awk -F, '{print $7"," $8"," $9"," $10"," $11"," $12"," $13"," $14"\n"}' 

then I have this code for the whole 100 line files in source.sh:

#I want to change 6th value for every line of source.csv to current date and keep the rest and export it to output.csv
while read  
do
part1=$(`cat source.csv | awk -F, '{ print $1"," $2"," $3"," $4"," $5","}'`)
datum=$(`date`) 
part2=$(`cat source.csv |awk -F, '{print $7"," $8"," $9"," $10"," $11"," $12"," $13"," $14"\n"}'`)
echo `$part1 $datum $part2`
done

and I expect to run the command like ./source.sh > output.csv

What I want for the full 100 lines file is to have result like: Food,Offline,H,Thu Jan 17 06:34:03 EST 2019,669165933,6/27/2010,9925,255.28,159.42,2533654.00,1582243.50,951410.50

Could you guide me how to change the code to get the result?

Inian
  • 80,270
  • 14
  • 142
  • 161
Andrej H
  • 33
  • 3

1 Answers1

3

Refactor everything to a single Awk script; that also avoids the echo in backticks.

awk -v datum="$(date)" -F , 'BEGIN { OFS=FS }
    { $6 = datum } 1' source.csv >output.csv

Briefly, we split on comma (-F ,) and replace the value of the sixth field with the value of the variable we passed in with -v. OFS=FS sets the output field separator to the input field separator (comma). Then the 1 means "print unconditionally".

Generally speaking, you should probably avoid while read.

Tangentially, your quoting looks wacky; you don't want backticks around $part1 unless it is a command you want the shell to run (which in turn is probably a bad idea in itself). Also, backticks have long been deprecated in favor of $(command) syntax which is more legible and offers some syntactic advantages.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you very much. That was amazing how fast you gave me the correct answer. I just need to figure out yet, how to put there "," instead of " " between the columns and the output.csv file will be correct. Thanks again. – Andrej H Jan 17 '19 at 12:17
  • The edit to add `OFS=FS` fixes that. It was already fixed by the time you commented. – tripleee Jan 17 '19 at 12:29
  • Thanks for the edit, now it works perfectly also with the "," as delimitor. – Andrej H Jan 17 '19 at 12:30