linux中正则表达式仅保留绝对路径的目录
001、 方法1
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试文件 /home/test2/a.txt [root@pc1 test2]# sed -r 's/(\/.*\/).*/\1/' a.txt ## 仅保留路径 /home/test2/
002、方法2:
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试数据 /home/test2/a.txt [root@pc1 test2]# sed 's/[^/]\+$//' a.txt ## 提取路径 /home/test2/
003、方法3:
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试数据 /home/test2/a.txt [root@pc1 test2]# sed 's/\S[^/]\+$//' a.txt ## 提取路径 /home/test2
004、方法4
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试文件 /home/test2/a.txt /home/test2/k.csv /home/test2/dir01/dirxx/xx.map [root@pc1 test2]# awk -F "/" '{OFS = "/";$NF = ""; print $0}' a.txt ## 提取路径 /home/test2/ /home/test2/ /home/test2/dir01/dirxx/
005、方法5
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试文件 /home/test2/a.txt /home/test2/k.csv /home/test2/dir01/dirxx/xx.map [root@pc1 test2]# awk -F "/" 'NF{NF--; OFS = "/"}1' a.txt ## 提取路径 /home/test2 /home/test2 /home/test2/dir01/dirxx
006、方法6
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试文件 /home/test2/a.txt /home/test2/k.csv /home/test2/dir01/dirxx/xx.map [root@pc1 test2]# cat a.txt | xargs -n 1 dirname ## 提取路径 /home/test2 /home/test2 /home/test2/dir01/dirxx
。