0

Input file: Contains 3 columns with the 3rd column is date in DD-MMM-YYYY format
sample data:

1232,abdc, 02-Jan-2014
4534,kdafh, 20-Feb-2014
364,asjhdk, 11-Jul-2012

required output:

1232,abdc, 2014-01-02
4534,kdafh, 2014-02-20
364,asjhdk, 2012-07-11

using function date: date -d 20-DEC-2014 +%Y-%m-%d
used it awk command gives error
Is there any other way?

  • 3
    How shall we help you if you don't tell us the exact command you used (and the error message)? – Hauke Laging Jan 7 '15 at 5:46
2

One possible answer using awk, assuming the input file is input-file.txt and output file is output-file.txt :

awk -F ',' '{printf("%s,%s,",$1,$2);system("date -d "$3" +%Y-%m-%d");}' input-file.txt > output-file.txt
  • The input file has a time stamp in the date field so the about code is give error. So input file has changed as below,1232,abdc, 02-Jan-2014 18:01:37</br> 4534,kdafh, 20-Feb-2014 07:17:19</br> 364,asjhdk, 11-Jul-2012 23:20:30</br> – user_tmo Jan 7 '15 at 20:30
0

Assuming you input text is like this:

1232,abdc, 02-Jan-2014 18:01:37</br> 
4534,kdafh, 20-Feb-2014 07:17:19</br>
364,asjhdk, 11-Jul-2012 23:20:30</br>

my answer would become:

cat input-file.txt | sed 's[</br>[[g' | awk -F ',' '{printf("%s,%s,",$1,$2);system("date -d \""$3" "$4"\" +\"%Y-%m-%d %H:%M:%S\"");}' > output-file.txt

And if the records are on a single line (no new line in input file after the </br>, it woukd be

cat input-file.txt | sed 's[</br>[\n[g' | awk -F ',' '{printf("%s,%s,",$1,$2);system("date -d \""$3" "$4"\" +\"%Y-%m-%d %H:%M:%S\"");}' > output-file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.