bash 如何 sed/grep 文件名中的最后一个单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8547558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I sed/grep the last word in a filename?
提问by AlMehdi
I have a couple of filenames for different languages. I need to grep or sed just the language part. I am using gconftool-2 -R /and want to pipe a command to bring out just the letters with the language.
我有几个不同语言的文件名。我只需要 grep 或 sed 语言部分。我正在使用gconftool-2 -R /并希望通过管道发送一个命令以仅带出该语言的字母。
active = file.so,sv.xml
active = file.so,en_GB.xml
active = file.so,en_US.xml
I need the svand en_GBpart of the file. How can I do that in the most effective way? I am thinking of something like gconftool-2 -R / | sed -n -e '/active =/p??' -e '/\.$/'but then I get stuck as I don't know how to print just the part I need and not the whole line.
我需要文件的sv和en_GB部分。我怎样才能以最有效的方式做到这一点?我正在考虑类似的事情,gconftool-2 -R / | sed -n -e '/active =/p??' -e '/\.$/'但后来我卡住了,因为我不知道如何只打印我需要的部分而不是整条线。
回答by Martin Beckett
awk -F. '{print $(NF-1)}'
NF is the number of fields, awk counts from 1 so the 2nd to last field is NF-1.
The -F. says that fields are separated by "." rather than whitespace
NF 是字段数,awk 从 1 开始计数,所以倒数第二个字段是 NF-1。
-F。表示字段由“。”分隔。而不是空格
回答by jaypal singh
How about using simple cut
如何使用简单 cut
cut -d. -f3 filename
Test:
测试:
[jaypal:~/Temp] cat filename
active = file.so.sv.xml
active = file.so.en_GB.xml
active = file.so.en_US.xml
[jaypal:~/Temp] cut -d. -f3 filename
sv
en_GB
en_US
Based on the updated input:
基于更新的输入:
[jaypal:~/Temp] cat filename
active = file.so,sv.xml
active = file.so,en_GB.xml
active = file.so,en_US.xml
[jaypal:~/Temp] cut -d, -f2 filename | sed 's/\..*//g'
sv
en_GB
en_US
OR
或者
Using awk:
使用awk:
[jaypal:~/Temp] awk -F[,.] '{print }' filename
sv
en_GB
en_US
[jaypal:~/Temp] awk -F[,.] '{print $(NF-1)}' filename
sv
en_GB
en_US
OR
或者
Using grepand tr:
使用grep和tr:
[jaypal:~/Temp] egrep -o ",\<.[^\.]*\>" f | tr -d ,
sv
en_GB
en_US
回答by Fredrik Pihl
awkwould be my main tool for this task but since that has already been proposed, I'll add a solution using cutinstead
awk将是我完成这项任务的主要工具,但由于已经提出了,我将添加一个解决方案,cut而不是使用
cut -d. -f3
i.e. use .as delimiter and select the third field.
即.用作分隔符并选择第三个字段。
Since you tagged the question with bash, I'll add a pure bash solution as well:
由于您用 标记了问题bash,我还将添加一个纯 bash 解决方案:
#!/usr/bin/bash
IFS=.
while read -a LINE;
do
echo ${LINE[2]}
done < file_name
回答by potong
This might work for you:
这可能对你有用:
gconftool-2 -R / | sed -n 's/^active.*,\([^.]*\).*//p'
回答by fge
Try:
尝试:
gconftool-2 -R / | grep '^active = ' | sed 's,\.[^.]\+$,,; s,.*\.,,'
gconftool-2 -R / | grep '^active = ' | sed 's,\.[^.]\+$,,; s,.*\.,,'
The first sedcommand says to remove a dot followed by everything not a dot until the end of line; the second one says to remove everything until the last dot.
第一个sed命令说删除一个点,然后是所有不是点的东西,直到行尾;第二个说要删除所有内容,直到最后一个点。

