删除前一个/bash之前的所有内容 [英] Delete everything before last / in bash

查看:64
本文介绍了删除前一个/bash之前的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件中的文件路径很多,如下所示:

I have many file paths in a file that look like so:

/home/rtz11/files/testfiles/547/prob547455_01

我想使用一个bash脚本,该脚本仅将所有文件名打印到屏幕上.所以基本上在最后一个/之后.我不想假设它的长度总是一样的,因为可能不一样.有没有办法删除最后一个/之前的所有内容?也许sed命令?任何帮助将不胜感激!

I want to use a bash script that will print all the filenames only to the screen. so basically after the last /. I don't want to assume that it would always be the same length because it might not be. Would there be a way to delete everything before the last /? Maybe a sed command? Any help would be greatly appreciated!

推荐答案

awk '{print $NF}' FS=/ input-file

'print $ NF'指示awk打印每行的最后一个字段,并分配FS =/来使字段分度符正斜杠.在sed中,您可以执行以下操作:

The 'print $NF' directs awk to print the last field of each line, and assigning FS=/ makes forward slash the field delimeter. In sed, you could do:

sed 's@.*/@@' input-file

只是删除所有内容,包括最后一个/.

which simply deletes everything up to and including the last /.

这篇关于删除前一个/bash之前的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆