bash 从列剪切到行尾

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

Cut from column to end of line

bashunix

提问by KyleCrowley

I'm having a bit of an issue cutting the output up from egrep. I have output like:

我在切断 egrep 的输出时遇到了一些问题。我有这样的输出:

From: First Last
From: First Last
From: First Last

I want to cut out the "From: " (essentially leaving the "First Last").

我想删除“发件人:”(基本上留下“第一个”)。

I tried

我试过

cut -d ":" -f 7

but the output is just a bunch of blank lines.

但输出只是一堆空行。

I would appreciate any help.

我将不胜感激任何帮助。

Here's the full code that I am trying to use if it helps:

如果有帮助,这是我尝试使用的完整代码:

egrep '^From:' $file | cut -d ":" -f 7

NOTE: I've already tested the egrep portion of the code and it works as expected.

注意:我已经测试了代码的 egrep 部分,它按预期工作。

回答by Aaron Miller

The cutcommand lines in your question specify colon-separated fields and that you want the output to consist only of field 7; since there is no 7th field in your input, the result you're getting isn't what you intend.

cut您问题中的命令行指定以冒号分隔的字段,并且您希望输出仅包含字段 7;由于您的输入中没有第 7 个字段,因此您得到的结果不是您想要的。

Since the "From:" prefix appears to be identical across all lines, you can simply cut from the 7th character onward:

由于“From:”前缀在所有行中似乎都相同,您可以简单地从第 7 个字符开始:

egrep '^From:' $file | cut -c7-

and get the result you intend.

并得到你想要的结果。

回答by grim

The -fargument is for what fields. Since there is only one :in the line, there's only two fields. So changing -f 7to -f 2-will give you want you want. Albeit with a leading space.

-f参数是哪些领域。由于:一行中只有一个,因此只有两个字段。所以改变-f 7-f 2-会给你想要的。尽管有领先的空间。

回答by Alok--

You can combine the egrepand cutparts into one command with sed:

您可以将egrepcut部分组合成一个命令sed

sed -n 's/^From: //gp' $file

sed -nturns off printing by default, and then I am using pin the sedcommand explicitly to print the lines I want.

sed -n默认情况下关闭打印,然后我psed命令中明确使用来打印我想要的行。

回答by anubhava

You can use sed:

您可以使用 sed:

sed 's/^From: *//'

OR awk:

或 awk:

awk -F ': *' '=="From"{print }'

OR grep -oP

或者 grep -oP

grep -oP '^From: *\K.*'

回答by Johan Alexis Duque Cadena

you were really close.

你真的很亲近。

I think you only need to replace ":" with " " as separator and add "-" after the "7": like this:

我认为您只需要将“:”替换为“”作为分隔符并在“7”后添加“-”:像这样:

cut -d " " -f 2-

I tested and works pretty well.

我测试过,效果很好。

回答by Akhil Maskery

cutitself is a very handy tool in bash

cut本身是 bash 中一个非常方便的工具

cut -d (delimiter character) -f (fields that you want as output)

cut -d(分隔符)-f(您想要作为输出的字段)

a single field is given directly as -f 3 , range of fields can be selected as -f 5-9

单个字段直接指定为 -f 3 ,字段范围可以选择为 -f 5-9

so in your this particular case code would be

所以在你这个特殊情况下,代码将是

egrep '^From:' $file | cut -d\ -f 2-3

egrep '^From:' $file | 剪切 -d\ -f 2-3

the delimiter is space here and can be escaped using a \

分隔符在这里是空格,可以使用\进行转义

-f 1 corresponds to " From " and 2-3 corresponds to " First Last "

-f 1 对应“From”,2-3 对应“First Last”

回答by kenorb

Here is a Bash one-liner:

这是一个 Bash one-liner:

grep ^From file.txt | while read -a cols; do echo ${cols[@]:1}; done

See: Handling positional parametersat wiki.bash-hackers.org

请参阅:在 wiki.bash-hackers.org处理位置参数