简体   繁体   English

如何删除文本文件中第一个空格后每行中的所有字符?

[英]How can I remove all characters in each line after the first space in a text file?

I have a large log file from which I need to extract file names. 我有一个大型日志文件,我需要从中提取文件名。

The file looks like this: 该文件如下所示:

/path/to/loremIpsumDolor.sit /more/text/here/notAlways/theSame/here
/path/to/anotherFile.ext /more/text/here/differentText/here
.... about 10 million times

I need to extract the file names like this: 我需要提取这样的文件名:

loremIpsumDolor.sit
anotherFile.ext

I figure my first strategy is to find/replace all /path/to/ with ''. 我认为我的第一个策略是找到/替换所有/path/to/ with''。 But I'm stuck how to remove all characters after the space. 但我坚持如何删除空格后的所有字符。

Can you help? 你能帮我吗?

sed 's/ .*//' file

It doesn't take any more. 它不再需要了。 The transformed output appears on standard output, of course. 当然,转换后的输出出现在标准输出上。

传递它cut

cut '-d ' -f1 yourfile

In theory, you could also use awk to grab the filename from each line as: 理论上,你也可以使用awk从每一行获取文件名:

awk '{ print $1 }' input_file.log

That, of course, assumes that there are no spaces in any of the filenames. 当然,这假定任何文件名中都没有空格。 awk defaults to looking for whitespace as the field delimiters, so the above snippet would take the first "field" from your log file (your filename) for each line, and output it. awk默认以字段分隔符的形式查找空格,因此上面的代码段会从每个行的日志文件(您的文件名)中获取第一个“字段”,然后输出它。

a bash-only solution: 仅限bash的解决方案:

while read path otherstuff; do
    echo ${path##*/}
done < filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM