Transform an entire column using "date" command

san*_*a16 1 linux bash awk date sed

Here is a dummy CSV file with 3 rows. The actual file has 7 million rows.

testdates.csv:

y_m_d
1997-01-01
1985-06-09
1943-07-14
Run Code Online (Sandbox Code Playgroud)

The date tool can usually be formatted as such , to get the 'day' :

date -d "25 JUN 2011" +%A
Run Code Online (Sandbox Code Playgroud)

=> output: Saturday

Query: How to provide an entire column as input for the date +%A transformation?

The resulting output should be appended to the end of the input file.

Intended output:

y_m_d, Day
1997-01-01, Thursday
1985-06-09, Sunday
1943-07-14, Tuesday
Run Code Online (Sandbox Code Playgroud)

Ben*_* W. 6

To read multiples dates from a file using GNU date, you can use the -f/--file option:

$ date -f testdates.csv '+%F, %A'
1997-01-01, Wednesday
1985-06-09, Sunday
1943-07-14, Wednesday
Run Code Online (Sandbox Code Playgroud)

Since your file has a header row, we have to skip that, for example using process substitution and sed:

date -f <(sed '1d' testdates.csv) '+%F, %A'
Run Code Online (Sandbox Code Playgroud)

To get your desired output, combine like this:

$ date -f testdates.csv '+%F, %A'
1997-01-01, Wednesday
1985-06-09, Sunday
1943-07-14, Wednesday
Run Code Online (Sandbox Code Playgroud)

or write to a new file:

date -f <(sed '1d' testdates.csv) '+%F, %A'
Run Code Online (Sandbox Code Playgroud)

检查后,您可以使用

mv testdates.csv.tmp testdates.csv
Run Code Online (Sandbox Code Playgroud)