Given below is the file content and the awk command used:
Input file:in_t.txt
1,ABC,SSS,20-OCT-16,4,1,0,5,0,0,0,0
2,DEF,AAA,20-JUL-16,4,1,0,5,0,0,0,0
Expected outfile:
SSS|2016-10-20,5
AAA|2016-07-20,5
I tried the below command:
awk -F , '{print $3"|"$(date -d 4)","$8}' in_t.txt
Got the outfile as:
SSS|20-OCT-16,5
AAA|20-JUL-16,5
Only thing I want to know is on how to format the date with the same awk command. Tried with
awk -F , '{print $3"|"$(date -d 4)","$8 +%Y-%m-%d}' in_t.txt
Getting syntax error. Can I please get some help on this?
Better to do this in shell itself and use
date -d
to convert thedate
format:From Unix.com
Just tweaked it a little to suit your needs
Explanation:
What's your definition of a single command? A call to awk is a single shell command. This may be what you want:
BTW it's important to remember that awk is not shell. You can't call shell tools (e.g.
date
) directly from awk any more than you could from C. When you wrote$(date -d 4)
awk saw an unset variable nameddate
(numeric value0
) from which you extracted the value of an unset variable namedd
(also0
) to get the numeric result0
which you then concatenated with the number4
to get04
and then applied the$
operator to to get the contents of field$04
(=$4
). The output has nothing to do with the shell commanddate
.This may work for you also.