awk 不打印换行符

发布于 2024-08-16 18:56:05 字数 629 浏览 4 评论 0原文

我希望变量 sum/NR 在每次迭代中并排打印。我们如何避免 awk 在每次迭代中打印换行符?在我的代码中,每次迭代中默认打印换行符

for file in cg_c ep_c is_c tau xhpl
printf "\n $file" >> to-plot.xls
    for f in 2.54 1.60 800 
        awk '{sum+=$3}; END  {print  sum/NR}' ${file}_${f}_v1.xls >> to-plot-p.xls
    done
done

我希望输出看起来像这样

cg_c ans1  ans2  ans3  
ep_c ans1  ans2  ans3 
is_c ans1  ans2  ans3
tau  ans1  ans2  ans3 
xhpl ans1  ans2  ans3

我当前的输出是这样的

**cg_c**
ans1
ans2
ans3
**ep_c**
ans1
ans2
ans3
**is_c**
ans1
ans2
ans3
**tau**
ans1
ans2
ans3
**xhpl**
ans1
ans2
ans3

I want the variable sum/NR to be printed side-by-side in each iteration. How do we avoid awk from printing newline in each iteration ? In my code a newline is printed by default in each iteration

for file in cg_c ep_c is_c tau xhpl
printf "\n $file" >> to-plot.xls
    for f in 2.54 1.60 800 
        awk '{sum+=$3}; END  {print  sum/NR}' ${file}_${f}_v1.xls >> to-plot-p.xls
    done
done

I want the output to appear like this

cg_c ans1  ans2  ans3  
ep_c ans1  ans2  ans3 
is_c ans1  ans2  ans3
tau  ans1  ans2  ans3 
xhpl ans1  ans2  ans3

my current out put is like this

**cg_c**
ans1
ans2
ans3
**ep_c**
ans1
ans2
ans3
**is_c**
ans1
ans2
ans3
**tau**
ans1
ans2
ans3
**xhpl**
ans1
ans2
ans3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

草莓酥 2024-08-23 18:56:05

awk '{sum+=$3}; END {printf "%f",sum/NR}' ${file}_${f}_v1.xls >>> to-plot-p.xls

print 默认情况下会插入换行符。您不希望发生这种情况,因此请使用 printf 代替。

awk '{sum+=$3}; END {printf "%f",sum/NR}' ${file}_${f}_v1.xls >> to-plot-p.xls

print will insert a newline by default. You dont want that to happen, hence use printf instead.

捎一片雪花 2024-08-23 18:56:05

AWK 中的 ORS(输出记录分隔符)变量默认为“\n”,并在每行之后打印。如果您希望连续打印所有内容,可以在 BEGIN 部分将其更改为“”。

The ORS (output record separator) variable in AWK defaults to "\n" and is printed after every line. You can change it to " " in the BEGIN section if you want everything printed consecutively.

迷雾森÷林ヴ 2024-08-23 18:56:05

我想很多人都在进入这个问题,寻找一种方法来避免 awk 中的新行。因此,我将为此提供一个解决方案,因为特定上下文的答案已经解决了!

awk 中,print 自动插入 <打印后代码>ORS。 ORS 代表“输出记录分隔符”,默认为新行。因此,每当您说 print "hi" awk 都会打印“hi”+新行。

这可以通过两种不同的方式进行更改:使用空的 ORS 或使用 printf。

使用空的 ORS

awk -v ORS= '1' <<< "hello
man"

这将返回“helloman”。

这里的问题是并非所有 awks 都接受设置空的 ORS,因此您可能必须设置另一个记录分隔符。

awk -v ORS="-" '{print ...}' file

例如:

awk -v ORS="-" '1' <<< "hello
man"

返回“hello-man-”。

使用printf(首选)

print 在记录后附加ORS,而printf 则不会。因此,printf "hello" 只打印“hello”,没有其他内容。

$ awk 'BEGIN{print "hello"; print "bye"}'
hello
bye
$ awk 'BEGIN{printf "hello"; printf "bye"}'
hellobye

最后,请注意,通常这会错过最后一个新行,因此 shell 提示符将与输出的最后一行位于同一行。要清除此内容,请使用END {print ""},以便在所有处理后打印新行。

$ seq 5 | awk '{printf "%s", $0}'
12345$
#    ^ prompt here

$ seq 5 | awk '{printf "%s", $0} END {print ""}'
12345

I guess many people are entering in this question looking for a way to avoid the new line in awk. Thus, I am going to offer a solution to just that, since the answer to the specific context was already solved!

In awk, print automatically inserts a ORS after printing. ORS stands for "output record separator" and defaults to the new line. So whenever you say print "hi" awk prints "hi" + new line.

This can be changed in two different ways: using an empty ORS or using printf.

Using an empty ORS

awk -v ORS= '1' <<< "hello
man"

This returns "helloman", all together.

The problem here is that not all awks accept setting an empty ORS, so you probably have to set another record separator.

awk -v ORS="-" '{print ...}' file

For example:

awk -v ORS="-" '1' <<< "hello
man"

Returns "hello-man-".

Using printf (preferable)

While print attaches ORS after the record, printf does not. Thus, printf "hello" just prints "hello", nothing else.

$ awk 'BEGIN{print "hello"; print "bye"}'
hello
bye
$ awk 'BEGIN{printf "hello"; printf "bye"}'
hellobye

Finally, note that in general this misses a final new line, so that the shell prompt will be in the same line as the last line of the output. To clean this, use END {print ""} so a new line will be printed after all the processing.

$ seq 5 | awk '{printf "%s", $0}'
12345$
#    ^ prompt here

$ seq 5 | awk '{printf "%s", $0} END {print ""}'
12345
╭ゆ眷念 2024-08-23 18:56:05

单程

awk '/^\*\*/{gsub("*","");printf "\n"$0" ";next}{printf $0" "}' to-plot.xls

one way

awk '/^\*\*/{gsub("*","");printf "\n"$0" ";next}{printf $0" "}' to-plot.xls
冰葑 2024-08-23 18:56:05

您可以像这样动态地使用 ORS:

awk '{ORS="" ;打印($1" "$2" "$3" "$4" "$5" "); ORS=“\n”; print($6-=2*$6)}' file_in >文件输出

You can simply use ORS dynamically like this:

awk '{ORS="" ; print($1" "$2" "$3" "$4" "$5" "); ORS="\n"; print($6-=2*$6)}' file_in > file_out

聆听风音 2024-08-23 18:56:05

如果可以选择 Perl,这里是使用 fedorqui 示例的解决方案:

seq 5 | perl -ne 'chomp;打印“$_”; END{print "\n"}'

说明:
chomp 删除换行符
print "$_ " 打印每一行,并附加一个空格
END{} 块用于打印换行符

输出:1 2 3 4 5

If Perl is an option, here is a solution using fedorqui's example:

seq 5 | perl -ne 'chomp; print "$_ "; END{print "\n"}'

Explanation:
chomp removes the newline
print "$_ " prints each line, appending a space
the END{} block is used to print a newline

output: 1 2 3 4 5

习ぎ惯性依靠 2024-08-23 18:56:05

这是 awk 方式,无需 printfEND(假设您的输入小于500 MB) ):

<前><代码>seq 199 |

mawk NF=NF RS='^$' FS='\n' OFS=

123456789101112131415161718192021222324252627282930313233343
536373839404142434445464748495051525354555657585960616263646
566676869707172737475767778798081828384858687888990919293949
596979899100101102103104105106107108109110111112113114115116
117118119120121122123124125126127128129130131132133134135136
137138139140141142143144145146147148149150151152153154155156
157158159160161162163164165166167168169170171172173174175176
177178179180181182183184185186187188189190191192193194195196
197198199

(为了可读性而重新格式化 - 它是一行,末尾有 \n

如果您只是想在之间放置一个空格:

<前><代码>seq 19 |

莫克'NF && --NF' RS='^$' FS='\n'

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

如果你不介意 1 个额外的尾随空格,那就更简单了

mawk NF=NF RS='^
FS='\n'

Here's the awk way without having to printf and END (assuming your input is less than, say,500 MB):

seq 199 | 

mawk NF=NF RS='^
123456789101112131415161718192021222324252627282930313233343
536373839404142434445464748495051525354555657585960616263646
566676869707172737475767778798081828384858687888990919293949
596979899100101102103104105106107108109110111112113114115116
117118119120121122123124125126127128129130131132133134135136
137138139140141142143144145146147148149150151152153154155156
157158159160161162163164165166167168169170171172173174175176
177178179180181182183184185186187188189190191192193194195196
197198199

(reformatted for readability - it's one single line, with \n at its end)

And if you simply wanna place a space in between :

seq 19 | 

mawk 'NF && --NF' RS='^
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

if you don't mind 1 extra trailing space, then it's even simpler

mawk NF=NF RS='^
FS='\n' OFS=

(reformatted for readability - it's one single line, with \n at its end)

And if you simply wanna place a space in between :

if you don't mind 1 extra trailing space, then it's even simpler

FS='\n'

if you don't mind 1 extra trailing space, then it's even simpler

FS='\n' OFS=

(reformatted for readability - it's one single line, with \n at its end)

And if you simply wanna place a space in between :

if you don't mind 1 extra trailing space, then it's even simpler

FS='\n' FS='\n' OFS=

(reformatted for readability - it's one single line, with \n at its end)

And if you simply wanna place a space in between :

if you don't mind 1 extra trailing space, then it's even simpler

FS='\n'

if you don't mind 1 extra trailing space, then it's even simpler

FS='\n' OFS=

(reformatted for readability - it's one single line, with \n at its end)

And if you simply wanna place a space in between :

if you don't mind 1 extra trailing space, then it's even simpler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文