reading a timestamp file and creating a directory

2019-08-18 02:11发布

问题:

I have log files for year's data , I want to read inside the log file and take the unix timestamp column and convert to human readable timestamp(not literally, only logically) . Depends on the timestamp . I want to create a Time based directory structure Something like this ( 2017/05/20/13 ) (YYYY/MM/DD/HH) and copy those files to respective directory structure

LANG=C
if [[ -z "$1" ]]
  then
if [[ -p /dev/stdin ]]    # input from a pipe
  then
    read -r p
 else
    echo "No timestamp given." >&2
    exit
 fi
  else
p=$1
fi
awk 'BEGIN {FS=OFS=":"}{$7=strftime("%Y-%m-%d %H:%M:%S",$7)} {print}' 
sample.txt

this is the Log file sample output after the awk command , Now I want to create a folder depends on the date and hour . Something like this

Input 
counter.txt
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:103
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:102
21:-60:17086:28:0:0:2017-05-24 15:12:50:1384:0:0:0:102
10:-64:16651:28:0:0:2017-05-24 16:12:50:656:0:0:0:103

folder structure be like [root@master 24-05-17]# ls -ltr | grep counter_ -rw-rw-r-- 1 user user 11974276 May 23 12:32 counter_25.txt -rw-rw-r-- 1 user user 17271141 May 23 13:32 counter_24.txt -rw-rw-r-- 1 user user 16819152 May 23 14:32 counter_23.txt

output is like :

/2017/05/24/15 - in this folder , we have to seperate the input file and write 
counter_2017_05_24_15.txt 
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:103
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:102
21:-60:17086:28:0:0:2017-05-24 15:12:50:1384:0:0:0:102

/2017/05/24/16 in this folder , I want to write the remaining data in another 
new file and store seperately under the folder 
counter_2017_05_24_16.txt
10:-64:16651:28:0:0:2017-05-24 16:12:50:656:0:0:0:103
23:-66:33444:28:0:0:2017-05-24 16:12:50:656:0:0:0:103
10:-64:16651:28:0:0:2017-05-24 16:12:50:656:0:0:0:102

回答1:

Instead of this:

strftime("%Y-%m-%d %H:%M:%S")

Do this:

strftime("%d/%m/%Y")

Now you have a DD/MM/YYYY string, and can use it as part of a path.



标签: linux shell sh