首页 > 解决方案 > 在文件扩展名后获取一个数字

问题描述

我有千兆字节的文件,具有以下命名约定:

H__Flights_SCP_Log_Analysis_Log_Store_Extracted_File_Store_Aircraft_023_Logs_06Apr2021_164418_dtd_slotb_MDN_Gateway_Logs_audit_audit.log.1

最后的数字(1)很重要,我需要抓住它。目前我的代码如下所示:

#!/bin/bash

#this grabs all the log files in the folder that will be converted and places it in a variable

files=$(find ./ -name "*.log*")

#I iterate through each file in files to convert each one

for file in $files
do

   #this grabs the file name except the file extension and places it in a variable
   name=${file%.*}
   #this converts the file and places it in a file with the same name plus a csv extension
   ausearch -if "file" --format csv>>$name.csv
done

这可以很好地转换日志并命名它们,只是它不获取文件扩展名末尾的数字。我怎么能抓住它?

标签: linuxbash

解决方案


继续使用 OP 当前使用参数替换...

$ file='H__Flights_SCP_Log_Analysis_Log_Store_Extracted_File_Store_Aircraft_023_Logs_06Apr2021_164418_dtd_slotb_MDN_Gateway_Logs_audit_audit.log.1'

$ mynum="${file##*.}"
$ echo "${mynum}"
1

# or

$ mynum="${file//*./}"
$ echo "${mynum}"
1

推荐阅读