awk 不打印换行符
我希望变量 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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 useprintf
instead.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.我想很多人都在进入这个问题,寻找一种方法来避免
awk
中的新行。因此,我将为此提供一个解决方案,因为特定上下文的答案已经解决了!在
awk
中,print
自动插入 <打印后代码>ORS。ORS
代表“输出记录分隔符”,默认为新行。因此,每当您说print "hi"
awk 都会打印“hi”+新行。这可以通过两种不同的方式进行更改:使用空的 ORS 或使用 printf。
使用空的 ORS
这将返回“helloman”。
这里的问题是并非所有 awks 都接受设置空的 ORS,因此您可能必须设置另一个记录分隔符。
例如:
返回“hello-man-”。
使用
printf
(首选)print
在记录后附加ORS
,而printf
则不会。因此,printf "hello"
只打印“hello”,没有其他内容。最后,请注意,通常这会错过最后一个新行,因此 shell 提示符将与输出的最后一行位于同一行。要清除此内容,请使用
END {print ""}
,以便在所有处理后打印新行。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 aORS
after printing.ORS
stands for "output record separator" and defaults to the new line. So whenever you sayprint "hi"
awk prints "hi" + new line.This can be changed in two different ways: using an empty
ORS
or usingprintf
.Using an empty
ORS
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.For example:
Returns "hello-man-".
Using
printf
(preferable)While
print
attachesORS
after the record,printf
does not. Thus,printf "hello"
just prints "hello", nothing else.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.单程
one way
您可以像这样动态地使用 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
如果可以选择 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 newlineprint "$_ "
prints each line, appending a spacethe
END{}
block is used to print a newlineoutput:
1 2 3 4 5
这是
awk
方式,无需printf
和END
(假设您的输入小于500 MB
) ):(为了可读性而重新格式化 - 它是一行,末尾有
\n
)如果您只是想在之间放置一个空格:
如果你不介意 1 个额外的尾随空格,那就更简单了
FS='\n'
Here's the
awk
way without having toprintf
andEND
(assuming your input is less than, say,500 MB
):(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' 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