Hi,
I have list of files as below, with prefix named as date & time. Anyone how to transform each file as below to yyyy-mm-dd hh:mm:ss
Regards,
Hi,
I have list of files as below, with prefix named as date & time. Anyone how to transform each file as below to yyyy-mm-dd hh:mm:ss
Regards,
chop out the date string from the file name and use
date +%F -d "<string>"
this will convert the string date to the yyyy-mm-dd format
It didn't work as expected
date +%F-d"`ll -lrt | awk '{print $9}' | cut -c1-6`"
August-d
080822
080822
080822
080822
080822
080822
Nothing wrong with the date solution -- you just need to run date one file at a time. (Use a for/while loop around ls -l). Meanwhile, perl's very handy to get the job done:
ls -1 | perl -n -e 'chop; print localtime( (stat($_))[9] ) . "\n";
The perl command processes each line of input (with -n option) and for each line, $_ is the input line. "chop" removes the newline character at the end of the line. "stat" retrieves the inode data from the filename (in $_). "localtime" will spit out the date/time of the file using the local timezone. (Use "gmtime" if you want it to ignore timezone settings.)
ls -E *.inp | awk '{print $6,$7}' | cut -d"." -f1
above command will give u the date and time as you expected..
U can try:
ls -l|while read file
do
file2="$(echo ${file}|sed -e "s/\(..\)\(..\)\(..\)_\(..\)\(..\)\(..\)/20\1-\2-\3 \4:\5\:\6/")"
mv "${file}" "${file2}"
done
Klashxx,
can u explain the above sed logic.
Every group of parenthesis \(..\) marks a group of characters in the filenames (because the first '_' character in the filename "anchors" the regular expression). In each group are two characters, which because of how you named your files, will be a number representing a portion of the date. After the second / (the substitution side), the back-quoted numbers (ie, \1 \2 \3, etc) refer "back" to the 1st, 2nd, 3rd group, respectively.
Put more succinctly, on the LHS of the 2nd /, the 2nd group of parenthesis matches exactly two characters and is represented on the RHS as \2. After sed does its work, your filenames will look like:
2008-08-22 05:00:01_FILE.TXT
what he said !