Hi, I have a column in a table of Timestamp datatype. For Example : Var1 is the column 2008-06-26-10.10.30.2006. I have Given query as date(var1) and time (var1) I got the file as in the below format :
File1:
Col1 Col2
2008-06-02|12.36.06
2008-06-01|23.36.35
But the problem is I need the output as
Col1 Col2
02062008|12:36:06
01022008|23:36:35
Thanks in advance.
`date +%Y%m%d`
will give ya date in YYYYMMDD format
`date +%H:%M:%S`
will give ya time in HH:MM:SS format
Hope this helps
awk can do the job:
$ cat File1
2008-06-02|12.36.06
2008-06-01|23.36.35
$ awk -F'[-|.]' '{printf "%s%s%s%c%s%c%s%c%s\n",$3,$2,$1,124,$4,58,$5,58,$6}' File1
02062008|12:36:06
01062008|23:36:35
echo '2008-06-02|12.36.06' | nawk -F'([|]|[-])' '{gsub("[.]", ":", $4); print $3$2$1 "|" $4}'
Thanks Danmero and vgresh99 for the reply. But still I have some another problem. If I have my file as
File1:
Col1 Col2 ID
2008-06-02|12.36.06|1-432
2008-06-01|23.36.35|1-345
In the above file I have ID column values as 1<<Hypen>>432,1<<hypen>>345.
What's happening is when I give the command
$ cat File1
$ nawk -F'([|]|[-])' '{gsub("[.]", ":", $4); print $3$2$1 "|" $4 "|" $5
It is giving output as :
Col1 Col2 ID
2008-06-02|12.36.06|1|432
2008-06-01|23.36.35|1|345
It is replacing 1<<Hypen>>432 as 1|432. Please let me know how to get out of it. Thanks in advance.
Below code is more flexible and can accommodate for more fields in the data input:
$ cat b.sh
#! /bin/sh
if [ $# -ne 1 ]; then
echo "Usage: $0 <input.txt>"
exit 1
fi
awk '
BEGIN {
FS="|"
}
{
split($1,ymd,"-")
split($2,hms,".")
d=sprintf("%s%s%s",ymd[3],ymd[2],ymd[1])
t=sprintf("%s:%s:%s",hms[1],hms[2],hms[3])
printf("%s|%s",d,t)
for(i=3;i<=NF;++i) {
printf("|%s",$i)
}
printf("\n")
}' $1
$ cat b.in
2008-06-02|12.36.06|32-143|1234.23
2008-06-01|23.36.35|12-239|99.45
$ ./b.sh b.in
02062008|12:36:06|32-143|1234.23
01062008|23:36:35|12-239|99.45
#!/usr/bin/ksh93
IFS='|'
while read col1 col2 col3
do
printf "%(%d%m%Y|%T)T|%s\n" "$col1 $col2" $col3
done < file
$./t
02062008|12:36:06|1-432
01062008|23:36:35|1-345
$
I think Chihung's input will be good. I tried it before. It worked for me.