我正在编写一个用于比较档案的程序。我在编辑字符串时遇到问题。我尝试用正则表达式编辑它。
我只需要编辑这一行
archive1\sample\nothing.txt
并且只获取没有子目录的文件名。像那样
nothing.txt
我尝试使用此表达式编辑文本,但似乎无效。
expr " archive1\sample\nothing.txt" : '\([a-z]*["."]*[a-z]\)'
答案 0 :(得分:4)
只需使用\
作为分隔符并打印最后一个块:
$ echo "archive1\sample\nothing.txt" | awk -F"\\" '{print $NF}'
nothing.txt
或者使用Bash,使用字符串表达式删除最后\
之前的所有内容:
$ r="archive1\sample\nothing.txt"
$ echo ${r##*\\}
nothing.txt
答案 1 :(得分:1)
basename
程序从文件名中删除目录:
$ basename 'archive1\sample\nothing.txt'
nothing.txt