bob123
1
need some more help please
i have a large file and
i want to change just the date format
with awk or sed
from this
yyyy-mm-dd
2016-04-15 8.30
2016-04-15 7.30
2016-04-13 7.30
2016-04-11 8.30
2016-04-11 7.30
2016-04-08 8.30
2016-04-08 7.30
2016-04-06 7.30
2016-04-04 8.30
to this
dd-mm-yyyy
15-04-2016 8.30
15-04-2016 7.30
13-04-2016 7.30
11-04-2016 8.30
11-04-2016 7.30
08-04-2016 8.30
08-04-2016 7.30
06-04-2016 7.30
04-04-2016 8.30
leaving the times as they are
thanks
rdrtx1
2
try:
sed 's/\([0-9][0-9][0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]\)/\3-\2-\1/' infile
2 Likes
drl
4
Hi.
Using a utility from the package dateutils, dconv
:
#!/usr/bin/env bash
# @(#) s1 Demonstrate date manipulation, format changes, dconv.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dateutils.dconv
FILE=${1-data1}
pl " Input data file $FILE:"
cat $FILE
pl " Expected output:"
cat expected-output.txt
# 2016-04-15 8.30 -> 15-04-2016 8.30
pl " Results:"
dateutils.dconv -S -i "%F" -f "%d-%m-%Y" < $FILE
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8 (jessie)
bash GNU bash 4.3.30
dateutils.dconv dconv 0.3.1
-----
Input data file data1:
2016-04-15 8.30
2016-04-15 7.30
2016-04-13 7.30
2016-04-11 8.30
2016-04-11 7.30
2016-04-08 8.30
2016-04-08 7.30
2016-04-06 7.30
2016-04-04 8.30
-----
Expected output:
15-04-2016 8.30
15-04-2016 7.30
13-04-2016 7.30
11-04-2016 8.30
11-04-2016 7.30
08-04-2016 8.30
08-04-2016 7.30
06-04-2016 7.30
04-04-2016 8.30
-----
Results:
15-04-2016 8.30
15-04-2016 7.30
13-04-2016 7.30
11-04-2016 8.30
11-04-2016 7.30
08-04-2016 8.30
08-04-2016 7.30
06-04-2016 7.30
04-04-2016 8.30
The dateutils package can be found in repositories Debian, MacOS ("brew"), SuSE, etc., and also at dateutils (verified 2016.04.16)
Best wishes ... cheers, drl
1 Like
Hello bob123,
Could you please try following and let me know if this helps you on same.
awk '{split($1, A,"-");$1=A[3] "-" A[2]"-" A[1]}; 1' Input_file
Output will be as follows.
15-04-2016 8.30
15-04-2016 7.30
13-04-2016 7.30
11-04-2016 8.30
11-04-2016 7.30
08-04-2016 8.30
08-04-2016 7.30
06-04-2016 7.30
04-04-2016 8.30
Thanks,
R. Singh
bob123
6
yes that works great as well
thank you