[英]remove part of string from each line
I have a text file, where each line is a single string of the format 我有一个文本文件,其中每一行都是格式的单个字符串
/home/usr1/284.txt
The whole file is like 整个文件就像
/home/usr1/284.txt
/home/usr1/361.txt
What I want is to remove /home/usr1/
and keep the file name, eg, 284.txt
我想要的是删除
/home/usr1/
并保留文件名,例如284.txt
How to do that using linux/unix command? 如何使用linux / unix命令做到这一点?
sed -e 's!/home/usr1/!!' filename.txt
or 要么
awk -F\/ {print $NF} filename.txt
should do the trick. 应该可以。 Note the use of
!
注意使用
!
instead of the more usual /
as pattern delimiters in the sed
example - it means you don't have to escape literal /
characters in your pattern. 而不是
sed
示例中更常见的/
作为模式分隔符-这意味着您不必在模式中转义文字/
字符。
Since the fields in the file are fixed, you can simply do: 由于文件中的字段是固定的,因此您可以简单地执行以下操作:
cut -b 12-
To skip the first 11 bytes of the input. 跳过输入的前11个字节。
您还可以使用Perl,如下所示:
perl -pe 's,.*/,,' file.txt
Try this: 尝试这个:
while read line; do basename "$line"; done < filename
The reciprocal of basename
is dirname
, in case you need the other part eventually. basename
的倒数是dirname
,以防最终需要其他部分。
There's more than one way to do it (TIMTOWTDI). 有多种方法可以做到这一点(TIMTOWTDI)。 In addition to the existing answers, I can think of two ways of going about this:
除了现有的答案,我可以想到两种解决方法:
Use slash "/" as a field delimiter: reverse, cut the first field, then reverse again: 使用斜杠“ /”作为字段定界符:反向,剪切第一个字段,然后再次反向:
< filename.txt rev | cut -d/ -f 1 | rev
These are filenames, hence you can use GNU basename
in combination with xargs
(or GNU parallel
) to, as you say, "keep the file name": 这些是文件名,因此您可以将GNU
basename
与xargs
(或GNU parallel
)结合使用,以“保持文件名”的方式进行操作:
< filename.txt xargs basename -a
or 要么
< filename.txt parallel -X basename -a
Got bash? 重击了吗?
read -d '' -a lines < input.txt
echo "${lines[@]##*/}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.