0
if [ -f $FILEPATH3 ]; then
#Will print the Header columns from properties file.
print $header >$CFILEPATH3
#To add rows in output file from input file .
awk -F\" 'BEGIN{OFS=FS;} {for(i=1;i<=NF;i=i+2){gsub(/,/,"~\t",$i);}}1' $FILEPATH3 > $TFILEPATH3

#Removes the footer,header and prints the columns as per mapping by picking column numbers from properties file 
cat $TFILEPATH3| sed '1d' | awk 'BEGIN { FS = "~\t"; OFS = ",";}{ DATE = date -d "$'$ms2'" "+%Y%m%d" } { printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",  $'$ms1', $DATE,$'$ms3',$'$ms4',$'$ms5',$'$ms6',$'$ms7',$'$ms8’,”MS”,$’$ms10',$'$ms11',$'$ms12',$'$ms13',$'$ms14',$'$ms15',$'$ms16',$'$ms17'}' >> $CFILEPATH3

In the above code, I'm trying to copy the data from the input file to output file. ms1, ms2 are the column positions of a CSV input file.

ms2 is the date with format mm/dd/yyyy and which is considered as variable. We need to convert the variable into YYYYMMDD format and write it into output file

In the script I'm trying to change the date format to YYYYMMDD.. but I'm getting an error.

I think the error is from this code

{ DATE = date -d "$'$ms2'" "+%Y%m%d" }
1
  • in the properties file i have defined ms2=1 which means it has to pick the column 1 value from the input csv file. In input csv file, the column 1 will have date in the format of mm/dd/YYYY (example.. 6/03/2015).. i want to be formated to YYYYMMDD (20150603) while writing into output file.. Commented Jun 3, 2015 at 9:52

1 Answer 1

0

You are trying to access the variable in single quotes and which wont happen, So try to use the following syntax.

 DATE=$(echo $ms2|awk -F '/' '{print $3$2$1}')
cat $TFILEPATH3| sed '1d' | awk  -v ms2="$DATE" 'BEGIN { FS = "~\t"; OFS = ",";} { printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",  $'$ms1', $ms2,$'$ms3',$'$ms4',$'$ms5',$'$ms6',$'$ms7',$'$ms8’,”MS”,$’$ms10',$'$ms11',$'$ms12',$'$ms13',$'$ms14',$'$ms15',$'$ms16',$'$ms17'}' >> $CFILEPATH3
Sign up to request clarification or add additional context in comments.

5 Comments

it did not work.. the output was 0$$ms2+%Y%m%d .. but i want the date
Can you please provide an example format and syntax of command that you are using?
ms2 is the date with format mm/dd/yyyy and which is considered as variable. We need to convert the variable into YYYYMMDD format and write it into output file
In that case you can use awk command to convert the variable. echo 'mm/dd/yyyy'|awk -F '/' '{print $3$1$2}' I modified the answer , You can refer that.
i tried.. it did not change the format.. it still prints the date as mm/dd/YYYY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.