awk to strore string format in variable

In the below awk when I echo f it is empty, but if I remove the $f I get the desired results, however the new formatting is not stored in the $d variable. Basically I am trying to convert the string in $d variable into a new formatted variable $f. Thank you :).

file

ID,1A
DATE,220102

awk

d=$(awk -F, '/Date/ {print $2}' file) | f=$(date -d "$d" +'%Y-%m-%d')

f — desired —

2022-01-02

>Solution :

You need to use it this way to return a value from awk and set a shell variable:

f=$(date -d "$(awk -F, '/DATE/ {print $2}' file)" +'%Y-%m-%d')

echo "$f"
2022-01-02

Leave a Reply