我正在编写一个用于比较归档的程序。我在编辑字符串时遇到了问题。我试着用正则表达式来编辑它。
我只需要编辑这行
archive1samplenothing.txt
并且只获取不包含子目录的文件名。就像
nothing.txt
我试图用这个表达式编辑文本,但似乎不起作用。
expr " archive1samplenothing.txt" : '([a-z]*["."]*[a-z])'
只需使用作为分隔符并打印最后一个块:
$ echo "archive1samplenothing.txt" | awk -F"\" '{print $NF}'
nothing.txt
或者使用Bash,使用字符串表达式删除最后一个:之前的所有内容
$ r="archive1samplenothing.txt"
$ echo ${r##*\}
nothing.txt
basename
程序从文件名中剥离目录:
$ basename 'archive1samplenothing.txt'
nothing.txt