简体   繁体   English

AWK 命令打印到行尾

[英]AWK command to print until end of line

I have a quick question regarding the AWK command.我有一个关于 AWK 命令的快速问题。 I need the command to print until the end of the line on the same line, but then when it gets to the next line I need it to print on another line.我需要命令打印到同一行的行尾,但是当它到达下一行时,我需要它在另一行打印。 The following example will provide better clarity.以下示例将提供更好的清晰度。

Say I have a file:说我有一个文件:

0 1 2 3 This is line one
0 1 2 3 This is line two 
0 1 2 3 This is line three 
0 1 2 3 This is line four

I have tried the following and gotten the following results我已经尝试了以下并得到以下结果

awk '{for(i=5;i<=NF;i++) print $i}' fileName >> resultsExample1

I get the following in resultsExample1我在 resultsExample1 中得到以下结果

This
is
line
one
This 
is 
line 
two 
And so on....

Example 2:示例 2:

awk 'BEGIN {" "} {for(i=5;i<=NF;i++) printf $1}' fileName >> resultsExample2

for resultsExample2 I get:对于 resultsExample2 我得到:

This is line one This is line two this is line three This is line four

I have also tried:我也试过:

awk 'BEGIN {" "} {for(i=5;i<=NF;i++) printf $1}' fileName >> resultsExample3

But the results were the same as the previous one但是结果和上一个一样

In the end I want the following:最后,我想要以下内容:

This is line one
This is line two 
This is line three
This is line four

I'm grateful for any help!我很感激任何帮助! Thanks in advance :)提前致谢 :)

I know this question is very old, but another awk example:我知道这个问题很老了,但是另一个 awk 示例:

awk '{print substr($0,index($0,$5))}' fileName

What it does: find the index where you want to start printing (index of $5 in $0) and print the substring of $0 starting at that index.它的作用:找到要开始打印的索引($0 中的 $5 索引)并从该索引开始打印 $0 的子字符串。

OR with awk或 awk

awk '{$1=$2=$3=$4=""; sub(/^  */,"", $0); print }'  awkTest2.txt
This is line one
This is line two
This is line three
This is line four

Also, you're solution is almost there, you just need to force a '\\n' to be printed at the end of each processed line, ie此外,您的解决方案几乎就在那里,您只需要在每个处理行的末尾强制打印 '\\n',即

awk '{for(i=5;i<=NF;i++) {printf $i " "} ; printf "\n"}' awkTest2.txt
This is line one
This is line two
This is line three
This is line four

Note that your BEGIN { " " } is a no op.请注意,您的BEGIN { " " }不是操作。 And you should use $i instead of $1 to print the current iterations value.并且您应该使用$i而不是$1来打印当前迭代值。

IHTH. IHTH。

Edit ;编辑; Noting sudo_O objection, I added a %s to the data.注意到 sudo_O 反对,我在数据中添加了一个 %s。 Here is the output这是输出

This is line one
This is line two
This is line three
T%shis is line four

This may be a problem for you, so it that case read about how to pass a format string to printf.这对您来说可能是个问题,因此在这种情况下,请阅读有关如何将格式字符串传递给 printf 的信息。

It may be more straight-forward to use cut :使用cut可能更直接:

$ cut -d' ' -f5- file
This is line one
This is line two 
This is line three 
This is line four

This says: on space-separated fields, print from the 5th up to the end of the line.这表示:在空格分隔的字段上,从第 5 行到行尾打印。

If you happen to have multiple spaces in between fields, you may initially want to squeeze them with tr -s' ' .如果您碰巧在字段之间有多个空格,您最初可能想用tr -s' '挤压它们。

awk '{gsub (/[[:digit:]]/,"");{$1=$1}}1' file

sed provides the best solution to this problem . sed为这个问题提供了最好的解决方案

The accepted cut-based solution has the problem that, unlike awk, it assumes there is exactly one space between fields.公认的基于切割的解决方案存在的问题是,与 awk 不同,它假设字段之间恰好有一个空格。

The usual fix of using tr -s ' ' to squeeze multiple adjacent spaces into one space is problematic too: it would collapse spaces in the trailing remainder of the line, thus modifying it, as @inopinatus commented.使用tr -s ' '将多个相邻空格压缩到一个空格中的通常解决方法也有问题:它会折叠行尾剩余部分中的空格,从而修改它,正如@inopinatus 所评论的那样。

The following sed-based solution will achieve our goal while preserving spaces in the remainder of the line:以下基于 sed 的解决方案将实现我们的目标,同时保留该行其余部分的空间:

sed -E 's/^([^ \t]*[ \t]*){4}//' <<'EOF'
0 1 2 3 This is line one
0 1 2 3 This is line two   test of extra spaces
0 1 2 3 This is line three
0 1 2 3 This is line four
EOF

Result:结果:

This is line one
This is line two   test of extra spaces
This is line three
This is line four

We simulated awk's default behavior of delimiting fields by sequences of whitespace.我们模拟了 awk 通过空格序列分隔字段的默认行为。

Fields are normally separated by whitespace sequences (spaces, TABs, and newlines)字段通常由空格序列(空格、制表符和换行符)分隔
Default Field Splitting (The GNU Awk User's Guide) 默认字段拆分(GNU Awk 用户指南)

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

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