2

This question already has an answer here:

I would like to convert the date format in file, using a shell script.

Input:

S.no|Name|Joining_Date|Address
1|asdasd|09/18/2018|asdas
2|asd|09/18/2018|asdasd
3|asdas|09/18/2018|aadadw

Output:

S.no|Name|Joining_Date|Address
1|asdasd|18/09/2018|asdas
2|asd|18/09/2018|asdasd
3|asdas|18/09/2018|aadadw

Below are the scripts which have tried

Input File:
cat datesep.txt :

asdasd|asdasd|asdasd|30/08/2018|sadasd  
asdasd|asdasd|asdasd|12/09/2018|sadasd  
asdasd|asdasd|asdasd|23/06/2018|sadasd  
asdasd|asdasd|asdasd|26/10/2018|sadasd  
asdasd|asdasd|asdasd|23/09/2018|sadasd  
asdasd|asdasd|asdasd|12/07/2018|sadasd 

cat test.sh

#!/bin/bash  
date +'%d-%m-%y'  
DATE=`date +%d`  
MONTH=`date +%m`  
YEAR=`date +%y`  
echo "${MONTH}/${DATE}/${YEAR}"  

cat test1.sh

#!/bin/bash  
SOURCEDATE=`awk -F '|' '{print $4}' /home/work/datesep.txt`  
TEMP=$SOURCEDATE  
echo $TEMP  
DATE=`cut -c 1-2 ${TEMP}`  
MONTH=`cut -c 4-5 ${TEMP}`  
YEAR=`cut -c 7-10 ${TEMP}`  
echo "${MONTH}/${DATE}/${YEAR}"  

cat test2.sh

#!/bin/bash  
awk -F '|' '{print $4}' /home/work/datesep.txt > /home/work/temp.txt  
TEMP=$SOURCEDATE  
echo $TEMP  
DATE=`cut -c 1-2 /home/work/temp.txt`  
MONTH=`cut -c 4-5 /home/work/temp.txt`  
YEAR=`cut -c 7-10 /home/work/temp.txt`  
echo "${MONTH}/${DATE}/${YEAR}"  

cat test3.sh

#!/bin/bash  
awk -F '|' '{print $4}' /home/work/datesep.txt > /home/work/temp.txt  
TEMP=/home/work/temp.txt  
COUNT=`cat ${TEMP} | wc -l`  
echo $COUNT  
echo $TEMP  
LINE_NUM=1  
while read LINE  
do  
        DATE=`awk -F '/' '{print $1}' LINE`  
        MONTH=`awk -F '/' '{print $2}' LINE`  
        YEAR=`awk -F '/' '{print $3}' LINE`  
        echo "${MONTH}/${DATE}/${YEAR}"  
        ((LINE_NUM++))  
done < $TEMP 

cat test4.sh

#!/bin/bash  
awk -F '|' '{print $4}' /home/work/datesep.txt > /home/work/temp.txt  
TEMP=`/home/work/temp.txt`    
for LOOP in `/home/work/temp.txt `  
do  
        DATE=`awk -F '/' '{print $1}' $LOOP`  
        MONTH=`awk -F '/' '{print $2}' $LOOP`  
        YEAR=`awk -F '/' '{print $3}' $LOOP`  
        echo "${MONTH}/${DATE}/${YEAR}"  
done  

marked as duplicate by roaima, dr01, Jeff Schaller, jimmij, John WH Smith Sep 18 '18 at 19:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Hi! What have you tried this far, and where in that process are you stuck? – maulinglawns Sep 18 '18 at 10:17
  • 6
    I'd recommend you use the date format YYYY-MM-DD, as that ISO-standard format is unambiguous. – glenn jackman Sep 18 '18 at 10:22
  • 2
    Then please share what you have tried, there are many skilled awk hackers (I am not one of them!) around that can give you pointers. – maulinglawns Sep 18 '18 at 10:24
  • 2
    Welcome to Unix & Linux. ;-) We're not a script-writing service but we're always willing to help if you're stuck in scripting hell. Also see this – Fabby Sep 18 '18 at 10:36
  • 1
    Much better! +1 from me! (and you've got an answer in the meantime too as it's a clear question now) – Fabby Sep 18 '18 at 11:31
4

Working from the first set of given input and expected output in the question and assuming that you'd just want to swap the month and day in the date in the third |-delimited field.

$ awk -F '|' -v OFS='|' 'NR > 1 { split($3, a, "/"); $3 = sprintf("%s/%s/%s", a[2],a[1],a[3]) } 1' <file
S.no|Name|Joining_Date|Address
1|asdasd|18/09/2018|asdas
2|asd|18/09/2018|asdasd
3|asdas|18/09/2018|aadadw

This awk command treats each input line as a record with |-delimited fields. It takes the 3rd such field on each line (except the first line) and splits it on / into the array a. It then reassembles the field using the a array into the wanted format and order using sprintf() (which works like printf() but returns the generated string instead of outputting it). The lone 1 at the end causes each line to be outputted (it's a short way of writing { print }).

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