I can't believe I couldn't find my question anywhere else in the bash/linux community. I simply want to replace the first column of my csv file with a variable...
0,8,9,10
0,8,9,10
0,8,9,10
0,8,9,10
0,8,9,10
0,8,9,10
2/24/14,8,9,10
2/24/14,8,9,10
2/24/14,8,9,10
2/24/14,8,9,10
2/24/14,8,9,10
2/24/14,8,9,10
I simply just want to replace the first column with $date
, but all the awk commands don't allow a variable as a the replacement.
When working with a CSV file it is often necessary to find data contained within and sometimes replace it. Find & Replace is used for just this. You can access it from the Edit > Find & Replace menu or by pressing Ctrl-F on the keyboard.
The join() method takes all lines of a CSV file in an iterable and joins them into one string. Then, we can use replace() method on the entire string and can perform single/multiple replacements. In the entire string, the given text is searched and replaced with the specified text.
This should work:
awk -v dt="$date" 'BEGIN{FS=OFS=","}{$1=dt}1' inputFile
Explaination:
-v
option to set an awk
variable and assign it your shell
variable. ,
(since it is a csv)$1
to your awk
variable. 1
is to print the line with modified $1
. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With