Linux 如何使用 sed 或 awk 从路径中提取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13766329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:59:11  来源:igfitidea点击:

How to extract filename from path using sed or awk

linuxunixsedawk

提问by Mark McWhirter

I am trying to parse a filename from a modified apache web access log entry that is tab delimited:

我正在尝试从制表符分隔的修改后的 apache 网络访问日志条目中解析文件名:

/common/common/img/pictos/klArrowRight.gif    /common/common/img/pictos/klArrowRight.gif   03/Dec/2012:00:00:00    127.0.0.1   03/Dec/2012:00:00:00    us   404

I would like it to come out like this:

我希望它是这样出来的:

klArrowRight.gif    /common/common/img/pictos/klArrowRight.gif   03/Dec/2012:00:00:00    127.0.0.1   03/Dec/2012:00:00:00    us   404

I have tried something like this in sed:

我在sed 中尝试过这样的事情:

's:.*/::'

's:.*/::'

However, it is too greedy, and it eats the rest of my line. I have been looking through posts, but so far no luck. Any hints?

然而,它太贪婪了,它吃掉了我的其余部分。我一直在浏览帖子,但到目前为止没有运气。任何提示?

采纳答案by Kent

the input/output in your question is not well formatted. do you need this?

您问题中的输入/输出格式不正确。你需要这个吗?

awk '{gsub(/\/.*\//,"",); print}' file

test

测试

kent$  echo "/common/common/img/pictos/klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404"|awk '{gsub(/\/.*\//,"",); print}'

output:

输出:

klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404

回答by zb'

using perl regexp and basename (i not think you stuck on sed/awk):

使用 perl regexp 和 basename(我不认为你坚持使用 sed/awk):

perl -p -e 'use File::Basename;s/([^\s]+\s+)[^\s]+\s+//;print basename()'

example:

例子:

echo "/common/common/img/pictos/klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404" |
   perl -p -e 'use File::Basename;s/([^\s]+\s+)[^\s]+\s+//;print basename()'

klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404

回答by Ed Morton

awk 'BEGIN{FS=OFS="\t"} {sub(/.*\//,"",)} 1' file

回答by Norman Gray

You can do this pretty easily with just sed, as long as you tell it not to be too greedy:

只要你告诉它不要太贪婪,你就可以很容易地用 sed 做到这一点:

% echo '/img/pictos/klArrowRight.gif 03/Dec/2012' | sed 's,^[^ ]*/,,'
klArrowRight.gif 03/Dec/2012
%

(that is, "starting at the beginning of the line, find the longest-possible list of non-space characters, followed by a slash")

(即“从行首开始,找到最长可能的非空格字符列表,后跟斜杠”)

回答by Steve

One way using GNU grep:

一种使用方式GNU grep

grep -oP "[^/]*\t.*" file

Results:

结果:

klArrowRight.gif    /common/common/img/pictos/klArrowRight.gif  03/Dec/2012:00:00:00    127.0.0.1   03/Dec/2012:00:00:00    us  404

回答by user12531

None of the given answers seem to be correct completely when only the extraction of a filename from a given absolute path is desired. Therefore I give here the solution. Let's consider in variable filenamewe have the complete path, e.g., filename=/ABC/DEF/GHIThen,

当只需要从给定的绝对路径中提取文件名时,给出的答案似乎都不是完全正确的。因此我在这里给出了解决方案。让我们考虑在变量文件名中我们有完整的路径,例如,filename=/ABC/DEF/GHI然后,

echo $filename | awk 'BEGIN{FS="/"}{print $NF}'

will result in the filename GHI.

将导致文件名GHI