Operating System - HP-UX
1821544 Members
2339 Online
109633 Solutions
New Discussion юеВ

Re: How to change date format using sed or awk

 
SOLVED
Go to solution
Eric Leung_2
Advisor

How to change date format using sed or awk

Hi,

I've got a text file in the following format:

abcdefg|MM/DD/YY|00:30|xyzxyz
where MM=month, DD=day and YY=year

How can I change the format into:

abcdefg|YY/MM/DD|00:30|xyzxyz

using sed or awk?

Thanks in advance


10 REPLIES 10
Jose Mosquera
Honored Contributor

Re: How to change date format using sed or awk

Hi,

Try this script procedure:

TEXT=`cat text_file`
FIELD_1=`echo $TEXT|awk -F"|" '{ print $1 }'`
FIELD_2=`echo $TEXT|awk -F"|" '{ print $2 }'`
FIELD_3=`echo $TEXT|awk -F"|" '{ print $3 }'`
FIELD_4=`echo $TEXT|awk -F"|" '{ print $4 }'`
DATE=`echo $FIELD_2|awk -F"/" '{ print $3"/"$1"/"$2 }'`
echo "$FIELD_1|$DATE|$FIELD_3|$FIELD_4" > text_file

Rgds.
Steve Steel
Honored Contributor

Re: How to change date format using sed or awk

Here ia another way. file is parameter

filein=$1
cat $filein|while read linein
do
OLDIFS=$IFS
export IFS="|"
echo "$linein"|read a b c d
export IFS="/"
echo "$b"|read e f g
export IFS=$OLDIFS
echo "$a""|""$g""/""$e""/""$f""|""$c""|""$d"
done


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Eric Leung_2
Advisor

Re: How to change date format using sed or awk

Thanks for your replies and maybe I need to define my problem more clearly.

Actually, I need to change five different files and the number of fields varies from file to file. Unfortunately, there are more than 20 fields in some files.

Hence, it would be better if there is a function to handle all these files.

Thanks.
Muthukumar_5
Honored Contributor

Re: How to change date format using sed or awk

It is good to operate it with the awk. There are | and / can be used as field separators.

FLD="abcdefg|MM/DD/YY|00:30|xyzxyz"
echo "$(echo $FLD | awk -F "|" '{ print $1 }')|$(echo $FLD | awk -F "|" '{ print $2 }' | awk -F "/" '{ print $3"/"$2"/"$1 }')|$(echo $FLD | awk -F "|" '{ print $3"|"$4 }')"

It will give you the desired output.
Easy to suggest when don't know about the problem!
Mark Grant
Honored Contributor

Re: How to change date format using sed or awk

or

perl -pne 's#(..)/(..)/(..)#$3/$1/$2#' textfile > newfile
Never preceed any demonstration with anything more predictive than "watch this"
Muthukumar_5
Honored Contributor

Re: How to change date format using sed or awk

Get line by line from a file as,

fun1()
{
filename=$1
newfile=/tmp/test.log
rm -f $newfile
touch $newfile

while read line; do
echo line | grep -q "[[:alpha:]]*/[[:alpha:]]*/[[:alpha:]]"
if [[ $? -eq 0 ]]; then
operation of awk to change the format >> newfile
else
echo $file >>newfile
done < filename

mv $newfile $filename

}

fun1
...
fun1

It is good to have your file examples to make a scipt appropriate to ur requirement.
Easy to suggest when don't know about the problem!
Fred Ruffet
Honored Contributor

Re: How to change date format using sed or awk

Without sed, without awk ? just cut it :)

$>cat file
abcdefg|MM/DD/YY|00:30|xyzxyz
jdfds|07/20/04|HH:MM|jsfklm
fdslmkqjfq|06/15/03|23:14|jfds
$>for i in `cat file`
> do
> echo "`echo $i | cut -d '|' -f 1`|\c"
> echo "`echo $i | cut -d '|' -f 2 | cut -d '/' -f 2`/\c"
> echo "`echo $i | cut -d '|' -f 2 | cut -d '/' -f 1`/\c"
> echo "`echo $i | cut -d '|' -f 2 | cut -d '/' -f 3`|\c"
> echo "`echo $i | cut -d '|' -f 3,4`"
> done
abcdefg|DD/MM/YY|00:30|xyzxyz
jdfds|20/07/04|HH:MM|jsfklm
fdslmkqjfq|15/06/03|23:14|jfds

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
curt larson_1
Honored Contributor
Solution

Re: How to change date format using sed or awk

cat yourFile |
awk -F"|" '{
split($2,a,"/");
$2=sprintf("%s/%s/%s",a[3],a[1],a[2]);
print $0;
}'

i don't have my computer running to test that, but it should be pretty close to what you need.
curt larson_1
Honored Contributor

Re: How to change date format using sed or awk

sed would be something like this:

sed 's!|\(..\)/\(..\)/\(..\)|!|\3/\1/\2|!' yourFile
Rodney Hills
Honored Contributor

Re: How to change date format using sed or awk

If you want a function within "awk" so you can call it multiple times, here is a sample-

echo "hello|10/17/03|abc" | awk -F\| 'function swap(x) { split(x,a,"/"); return a[3] "/" a[2] "/" a[1] };{print swap($2)}'

You can call "swap" multiple times within your awk script. My sample is a one-line but you may want to put the awk script into a seperate file.

HTH

-- Rod Hills
There be dragons...