Parsing date and time format - Bash -



Parsing date and time format - Bash -

i have date , time format this(yearmonthday):

20141105 11:30:00

i need assignment year, month, day, hr , min values variable.

i can year, day , hr this:

year=$(awk '{print $1}' log.log | sed 's/^\(....\).*/\1/') day=$(awk '{print $1}' log.log | sed 's/^.*\(..\).*/\1/') hour=$(awk '{print $2}' log.log | sed 's/^\(..\).*/\1/')

how can month , minute?

--

and need every line of log file:

20141105 11:30:00 /bla/text.1 20141105 11:35:00 /bla/text.2 20141105 11:40:00 /bla/text.3 ....

i'm trying read line line log file , this:

mkdir -p "/bla/backup/$year/$month/$day/$hour/$minute" mv $file "/bla/backup/$year/$month/$day/$hour/$minute"

here not working code:

#!/bin/bash log=/var/log/log while read line year=${line:0:4} month=${line:4:2} day=${line:6:2} hour=${line:9:2} minute=${line:12:2} file=$(awk '{print $3}') if [ -f "$file" ]; printf -v path "%s/%s/%s/%s/%s" $year $month $day $hour $minute mkdir -p "/bla/backup/$path" mv $file "/bla/backup/$path" fi done < $log

you don't need phone call out awk date @ all, utilize bash's substring operations

d="20141105 11:30:00" yr=${d:0:4} mo=${d:4:2} dy=${d:6:2} hr=${d:9:2} mi=${d:12:2} printf -v dir "/bla/%s/%s/%s/%s/%s/\n" $yr $mo $dy $hr $mi echo "$dir" /bla/2014/11/05/11/30/

or directly, without variables.

printf -v dir "/bla/%s/%s/%s/%s/%s/\n" ${d:0:4} ${d:4:2} ${d:6:2} ${d:9:2} ${d:12:2}

given log file:

while read -r date time file; d="$date $time" printf -v dir "/bla/%s/%s/%s/%s/%s/\n" ${d:0:4} ${d:4:2} ${d:6:2} ${d:9:2} ${d:12:2} mkdir -p "$dir" mv "$file" "$dir" done < filename

or, making big assumption there no whitespace or globbing characters in filenames:

sed -r 's#(....)(..)(..) (..):(..):.. (.*)#mv \6 /blah/\1/\2/\3/\4/\5#' | sh

bash parsing awk sed

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -