[英]linux shell script to fetch a <latest file> of <particular name> in a folder
I'm enhancing my linux script, I have a folder with lots of files of different dates, I want to fetch a latest file starting with a particular name.我正在增强我的 linux 脚本,我有一个包含许多不同日期文件的文件夹,我想获取以特定名称开头的最新文件。
For ex.对于前。
I have below list of files in a folder I need latest file of name Su.network_RAN in a folder:我在文件夹中有以下文件列表我需要文件夹中名称为 Su.network_RAN 的最新文件:
Subnetwork_PCC_11Dec2022UTC0500
Subnetwork_RAN_12Dec2022UTC0500
Subnetwork_RAN_13Dec2022UTC0500
Subnetwork_PCC_13Dec2022UTC0500
Output will be file name Su.network_RAN_13Dec2022UTC0500 Output 将是文件名 Su.network_RAN_13Dec2022UTC0500
I tried to build a linux shell script to get latest file of particular name.我试图构建一个 linux shell 脚本来获取特定名称的最新文件。
This problem has a rather simple awk
solution:这个问题有一个相当简单的
awk
解决方案:
ls -tl | awk ' $9 ~ /Subnetwork_RAN/ {print $9; exit;}'
ls -tl
outputs a long listing of the current directory, sorted by time (newest first). ls -tl
输出当前目录的长列表,按时间排序(最新的在前)。
This output is piped to awk
which (line-by-line) looks for a filename containing the required string.此 output 通过管道传输到
awk
,后者(逐行)查找包含所需字符串的文件名。 The first time it finds one, it prints the filename and exits.第一次找到时,它会打印文件名并退出。
Note, this assumes (as in your example) that the filename contains no white space.请注意,这假定(如在您的示例中)文件名不包含空格。 If it does, you need to modify the print statement to print the substring of the line
$0
beginning with your string, to the end of the line.如果是这样,您需要修改 print 语句以将
$0
行的 substring 以您的字符串开头打印到该行的末尾。
If your string might be repeated in more recent filenames but not at the start, the regex condition can be modified to select only filenames where your string is at the start $9~/^Su.network_RAN/
如果您的字符串可能在最近的文件名中重复但不是在开头,则可以将正则表达式条件修改为 select 只有文件名,其中您的字符串在开头
$9~/^Su.network_RAN/
Supposing you have a file called test.txt
with the filenames you showed.假设您有一个名为
test.txt
的文件,其中包含您显示的文件名。 Then in bash you can do this:然后在 bash 你可以这样做:
awk 'BEGIN {FS="_"} $0 ~/Subnetwork_RAN/ {printf "%s ",$0; system("date +%s -d " $3)}' asd | sort -rn -k 2 | head -1 | cut -d " " -f 1
Output: Output:
Su.network_RAN_13Dec2022UTC0500
Some explanation:一些解释:
$0 ~ /Su.network_RAN/
matches all the lines containing the sub-string "Su.network_RAN" $0 ~ /Su.network_RAN/
匹配所有包含子字符串“Su.network_RAN”的行date
can recognize the date format like this 13Dec2022UTC0500 and transform it in a timestamp ( date +%s
) date
可以识别像这样的日期格式 13Dec2022UTC0500 并将其转换为时间戳( date +%s
)sort
sorts numerically in reverse order based on the second field (timestamp output of awk system
call) sort
根据第二个字段(awk system
调用的时间戳 output)以相反的顺序对数字进行排序head
gives the first line, ie, the most recent head
给出第一行,即最近的cut
takes only the first field given a field separator equal to " ".cut
仅采用第一个字段。 The first field is the full filename ( printf
call in awk)printf
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.