如何在bash中提取文件名的特定部分

如何在bash中提取文件名的特定部分

我有以下文件列表:

lab-1:/tmp/tmp.NMFaDO# ls                                                                                              
webserver.123.net.1970-01-01.csv          webserver.123.net.2016-10-01.csv          webserver.123.net.2017-10-01.csv                
webserver.123.net.2015-11-01.csv          webserver.123.net.2016-11-01.csv          webserver.123.net.2017-11-01.csv                
webserver.123.net.2015-12-01.csv          webserver.123.net.2016-12-01.csv          webserver.123.net.2017-12-01.csv                
webserver.123.net.2016-01-01.csv          webserver.123.net.2017-01-01.csv          webserver.123.net.2018-01-01.csv                
webserver.123.net.2016-02-01.csv          webserver.123.net.2017-02-01.csv          webserver.123.net.2018-02-01.csv                
webserver.123.net.2016-03-01.csv          webserver.123.net.2017-03-01.csv          webserver.123.net.current.csv                   
webserver.123.net.2016-04-01.csv          webserver.123.net.2017-04-01.csv          webserver.123.net.db.done                       
webserver.123.net.2016-05-01.csv          webserver.123.net.2017-05-01.csv          webserver.123.net.records_to_delete.txt         
webserver.123.net.2016-06-01.csv          webserver.123.net.2017-06-01.csv          sip2.ci2.lab.jwm2.net.2018-02-01.csv               
webserver.123.net.2016-07-01.csv          webserver.123.net.2017-07-01.csv          sip2.ci2.lab.jwm2.net.db.done                      
webserver.123.net.2016-08-01.csv          webserver.123.net.2017-08-01.csv          sip2.ci2.lab.jwm2.net.records_to_delete.txt        
webserver.123.net.2016-09-01.csv          webserver.123.net.2017-09-01.csv                                                             

在 bash 脚本中,我需要循环遍历此列表并仅提取文件名的日期部分。我设法弄清楚如何从
包含使用以下命令的文件的文件夹内部执行此操作:

lab-1:/tmp/tmp.NMFaDO# ls webserver.123.net*.csv | sed 's/webserver.123.net.\(.*\)\.csv/\1/'                     
1970-01-01                                                                                                                                   
2015-11-01                                                                                                                                   
2015-12-01                                                                                                                                   
2016-01-01                                                                                                                                   
2016-02-01                                                                                                                                   
2016-03-01                                                                                                                                   
2016-04-01                                                                                                                                   
2016-05-01                                                                                                                                   
2016-06-01                                                                                                                                   
2016-07-01                                                                                                                                   
2016-08-01                                                                                                                                   
2016-09-01                                                                                                                                   
2016-10-01                                                                                                                                   
2016-11-01                                                                                                                                   
2016-12-01                                                                                                                                   
2017-01-01                                                                                                                                   
2017-02-01                                                                                                                                   
2017-03-01                                                                                                                                   
2017-04-01                                                                                                                                   
2017-05-01                                                                                                                                   
2017-06-01                                                                                                                                   
2017-07-01                                                                                                                                   
2017-08-01                                                                                                                                   
2017-09-01                                                                                                                         
2017-10-01                    
2017-11-01                   
2017-12-01               
2018-01-01              
2018-02-01  
current

但我似乎无法让逻辑在我的 bash 脚本中工作:

dir=/tmp/tmp.NMFaDO
server=webserver.123.net
for csvfile in $dir/$server.*.csv
do 
  echo $csvfile | sed 's/$dir\/$server.\(.*\)\.csv/\1/'
done

目前它正在打印名称的完整路径,如下所示:

/tmp/tmp.NMFaDO/webserver.123.net.1970-01-01.csv
/tmp/tmp.NMFaDO/webserver.123.net.2015-11-01.csv

ETC。

有人能告诉我 bash 脚本中缺少什么吗?谢谢。

答案1

使用这样的文件名格式,您可以删除扩展名,然后删除最后一个点之前的所有内容。

带参数扩展:

for f in ./webserver.123.*; do
    d=${f%.csv}          # remove trailing .csv
    d=${d##*.}           # remove all up to (now) last .
    echo "filename: $f date: $d"
done

或使用 sed:

for f in ./webserver.123.*; do
    d=$(echo "$f" | sed -e 's/\.csv$//' -e 's/.*\.//');
    echo "filename: $f date: $d";
done

答案2

注意你使用的引号:所以

dir=/tmp/tmp.NMFaDO
server=webserver.123.net
for csvfile in $dir/$server.*.csv
do 
  echo "$csvfile" | sed "s|$dir/$server.\(.*\)\.csv|\1|"
done

“双引号”包含空格/元字符的每个文字以及每一个扩张:"$var""$(command "$var")""${array[@]}""a & b"。用于'single quotes'代码或文字$'s: 'Costs $5 US'ssh host 'echo "$HOSTNAME"'.看
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

相关内容