简体   繁体   English

如何从多个带有日期的文件中提取日期?

[英]How do i extract the date from multiple files with dates in it?

Lets say i have multiple filesnames eg R014-20171109-1159.log.20171109_1159. 可以说我有多个文件名,例如R014-20171109-1159.log.20171109_1159。 I want to create a shell script which creates for every given date a folder and moves the files matching the date to it. 我想创建一个Shell脚本,该脚本为每个给定的日期创建一个文件夹,并将与该日期匹配的文件移动到该文件夹​​。 Is this possible? 这可能吗?

For the example a folder "20171109" should be created and has the file "R014-20171109-1159.log.20171109_1159" on it. 例如,应创建一个文件夹“ 20171109”,并在其上放置文件“ R014-20171109-1159.log.20171109_1159”。

Thanks 谢谢

Below commands can be put in script to achieve this, 可以将以下命令放入脚本中以实现此目的,

Assign a variable with current date as below ( use --date='n day ago' option if need to have an older date). 分配一个具有当前日期的变量,如下所示(如果需要更早的日期,请使用--date ='n day ago'选项)。

if need to get it from File name itself, get files in a loop then use cut command to get the date string, 如果需要从文件名本身获取它,请循环获取文件,然后使用cut命令获取日期字符串,

dirVar=$(date +%Y%m%d) --> for current day,

dirVar=$(date +%Y%m%d --date='1 day ago') --> for yesterday,

dirVar=$(echo $fileName | cut -c6-13)   or
dirVar=$(echo $fileName | cut -d- -f2) --> to get from $fileName

Create directory with the variable value as below, ( -p : create directory if doesn't exist.) 使用以下变量值创建目录,(- p :如果不存在,则创建目录。)

mkdir -p ${dirVar}

Move files to directory to the directory with below line, 将文件移动到目录到下面一行的目录中,

mv *log.${dirVar}* ${dirVar}/

Since you want to write a shell script, use commands. 由于要编写Shell脚本,请使用命令。 To get date, use cut cmd like ex: 要获取日期,请像ex一样使用cut cmd:

cat 1.txt 
R014-20171109-1159.log.20171109_1159
 cat 1.txt | cut -d "-" -f2

Output 输出量

20171109 

is your date and create folder. 是您的日期并创建文件夹。 This way you can loop and create as many folders as you want 这样,您可以循环并创建任意数量的文件夹

Its actually quite easy(my Bash syntax might be a bit off) - 它实际上很容易(我的Bash语法可能有点不正确)-

for f in /path/to/your/files*; do

## Check if the glob gets expanded to existing files.
## If not, f here will be exactly the pattern above
## and the exists test will evaluate to false.
[ -e "$f" ] && echo $f > #grep the file name for "*.log." 
#and extract 8 charecters after "*.log." .
#Next check if a folder exists already with the name of 8 charecters. 
#If not { create}
#else just move the file to that folder path

break
done

Main idea is from this post link . 主要思想是从这个帖子链接 Sorry for not providing the actual code as i havent worked anytime recently on Bash 抱歉,由于我最近在Bash上任何时候都没有提供实际的代码,

This is a typical application of a for-loop in bash to iterate thru files. 这是bash中for-loop迭代文件的典型应用。 At the same time, this solution utilizes GNU [ shell param substitution ] . 同时,该解决方案利用了GNU [shell参数替换]

for file in /path/to/files/*\.log\.*
do

  foldername=${file#*-}
  foldername=${foldername%%-*}
  mkdir -p "${foldername}" # -p suppress errors if folder already exists
  [ $? -eq 0 ] && mv "${file}" "${foldername}" # check last cmd status and move

done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历多个文件以提取特定的列并另存为单独的文件? - How do I loop over multiple files to extract specific columns and save as separate files? 如何“提取”我提交的文件? - How do I “extract” my commited files? 如何从Linux中的多个xml文件中提取多个标记值 - How to extract multiple tag values from multiple xml files in linux 如何从多个文件夹中提取文件并根据文件夹名称 (bash) 重命名? - How can I extract files from multiple folders and rename according to folder name (bash)? 如何从tar.gz中仅提取所需的文件? - How do I extract only the desired files from tar.gz? 如何在我的 curl 脚本中只提取时间戳和日期? - How do I extract only timestamp and date at my curl script? 如何使用tar提取没有文件夹结构的文件 - How do I extract files without folder structure using tar 如何从多个文件中提取特定信息并在Linux中创建表? - How to extract specific information from multiple files and make a table in linux? 如何在Linux中读取开始日期和结束日期之间的所有日期 - How do I read all dates between start date and end date in linux 我需要通过删除文件前缀的多个日期来重命名多个文件 - I need to rename multiple files by removing the multiple dates prefixing the files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM