bash 打印 awk 中的其余字段

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

Print rest of the fields in awk

bashawk

提问by Shiplu Mokaddim

Suppose we have this data file.

假设我们有这个数据文件。

john 32 maketing executive
Hyman 41 chief technical officer
jim  27 developer
dela 33 assistant risk management officer

I want to print using awk

我想打印使用 awk

john maketing executive
Hyman chief technical officer
jim  developer
dela assistant risk management officer

I know it can be done using for.

我知道可以使用for.

awk '{printf ;  for(i=3;i<NF;i++){printf " %s", $i} printf "\n"}' < file

Problem is its long and looks complex.

问题是它很长而且看起来很复杂。

Is there any other short way to print rest of the fields.

有没有其他简短的方法来打印其余的字段

回答by Barun

Set the field(s) you want to skip to blank:

将要跳过的字段设置为空白:

awk '{ = ""; print 
$ gawk -v delNr=2 '{
$ gawk -v delNr=3 '{
awk '{sub( OFS, "")}1'
=gensub("^([[:space:]]*([^[:space:]]+[[:space:]]+){"delNr-1"})[^[:space:]]+[[:space:]]*","\1","")}1' file john 32 executive Hyman 41 technical officer jim 27 dela 33 risk management officer
=gensub("^([[:space:]]*([^[:space:]]+[[:space:]]+){"delNr-1"})[^[:space:]]+[[:space:]]*","\1","")}1' file john maketing executive Hyman chief technical officer jim developer dela assistant risk management officer
;}' < file_name

Source: Using awk to print all columns from the nth to the last

来源:使用 awk 打印从第 n 到最后的所有列

回答by Ed Morton

Reliably with GNU awk for gensub() when using the default FS:

使用默认 FS 时,使用 gensub() 可靠地使用 GNU awk:

awk '{=""}1' file

With other awks, you need to use match() and substr() instead of gensub(). Note that the variable delNr above tells awk which field you want to delete:

对于其他 awk,您需要使用 match() 和 substr() 而不是 gensub()。请注意,上面的变量 delNr 告诉 awk 您要删除哪个字段:

awk '{="";sub("  "," ")}1' file

Do not do this:

不要这样做:

awk '{=""}1' file

as the same text that's in $2 might be at the end of $1, and/or $2 might contain RE metacharacters so there's a very good chance that you'll remove the wrong string that way.

因为 $2 中的相同文本可能位于 $1 的末尾,和/或 $2 可能包含 RE 元字符,因此您很有可能会以这种方式删除错误的字符串。

Do not do this:

不要这样做:

awk '{sub( OFS, "")}1' file

as it adds an FS and will compress all other contiguous white space between fields into a single blank char each.

因为它添加了一个 FS 并将字段之间的所有其他连续空白压缩为一个空白字符。

Do not do this:

不要这样做:

tr -s ' ' < file | cut -d ' ' -f1,f3-

as it hasthe space-compression issue mentioned above and relies on a hard-coded FS of a single blank (the default, though, so maybe not so bad) but more importantly if there were spaces before $1 it would remove one of those instead of the space it's adding between $1 and $2.

因为它有上面提到的空间压缩问题,并且依赖于单个空白的硬编码 FS(虽然是默认值,所以可能还不错)但更重要的是,如果 $1 之前有空格,它将删除其中一个而不是它在 1 美元到 2 美元之间增加的空间。

One last thing worth mentioning is that in recent versions of gawk there is a new function named patsplit() which works like split() BUT in addition to creating an array of the fields, it also creates an array of the spaces between the fields. What that means is that you can manipulate fields and the spaces between then within the arrays so you don't have to worry about awk recompiling the record using OFS if you manipulate a field. Then you just have to print the fields you want from the arrays. See patsplit() in http://www.gnu.org/software/gawk/manual/gawk.html#String-Functionsfor more info.

最后值得一提的是,在最新版本的 gawk 中有一个名为 patsplit() 的新函数,它的工作方式与 split() 类似,但除了创建字段数组外,它还创建字段之间的空格数组。这意味着您可以在数组中操作字段和它们之间的空格,因此如果您操作字段,则不必担心 awk 使用 OFS 重新编译记录。然后你只需要从数组中打印你想要的字段。有关更多信息,请参阅http://www.gnu.org/software/gawk/manual/gawk.html#String-Functions 中的patsplit() 。

回答by anubhava

You can use simple awk like this:

您可以像这样使用简单的 awk:

tr -s ' ' < file | cut -d ' ' -f1 -f3-

However this will have an extra OFS in your output that can be avoided by this awk

但是,这将在您的输出中有一个额外的 OFS,可以通过这个 awk 避免

awk '{="";sub("  "," ")}1' file

OR else by using this tr and cut combo:

或者通过使用这个 tr 和 cut 组合:

On Linux:

在 Linux 上:

##代码##

On OSX:

在 OSX 上:

##代码##

回答by Jotne

This removes filed #2 and cleans up the extra space.

这将删除归档 #2 并清理额外的空间。

##代码##

回答by konsolebox

Another way is to just use sed to replace the first digits and space match:

另一种方法是仅使用 sed 替换第一个数字和空格匹配:

sed 's|[0-9]\+\s\+||' file

sed 's|[0-9]\+\s\+||' file