简体   繁体   English

如何在Mac的awk中做简单的concat

[英]how to do simple concat in awk in mac

So I have a file list.txt similar to this one: 所以我有一个与此类似的文件list.txt:

                              186
                              423
                              423
                              234
                              634
                              437

And I want an output similar to this: 我想要类似以下的输出:

SET 186 0
SET 423 0
SET 423 0
SET 234 0
SET 634 0
SET 437 0

I tried with this: 我尝试了这个:

sed 's/^ *//g' list.txt | awk '{a="SET ";b=" 0";print a,$0,b}'

But it prints 但它打印

  0 186
  0 423
  0 423
  0 234
  0 634
  0 437

Given that 鉴于

sed 's/^ *//g' list.txt | awk '{a="SET ";b=" 0";print a,$0}'

Works perfect: 完美的作品:

SET 186
SET 423
SET 423
SET 234
SET 634
SET 437

So I don't know what I'm doing wrong. 所以我不知道我在做什么错。

Do you know how to solve this and why print a,$0,b doesn't work? 您知道如何解决此问题,为什么print a,$0,b不起作用?

It doesn't work because your input file was created on Windows and so has spurious control-Ms at the end of each line and that's messing up the displayed output. 它不起作用,因为您的输入文件是在Windows上创建的,因此在每一行的末尾都有虚假的Control-M,这搞乱了显示的输出。 Run dos2unix on the file or otherwise get rid of those control-Ms and your script would work as written, but a better way to do it would be simply: 在文件上运行dos2unix或以其他方式摆脱那些control-M,您的脚本将按编写的方式运行,但更好的方法是:

awk '{print "SET",$1,0}' list.txt

使用awk,其:

awk '{printf "SET %d 0\n", $1}' your.file

Simply: 只是:

$ awk '{print "SET",$1,0}' file
SET 186 0
SET 423 0
SET 423 0
SET 234 0
SET 634 0
SET 437 0

try this awk one-liner: 试试这个awk单线:

awk '$0="SET "$1" 0"' file

outpus: 输出:

SET 186 0
SET 423 0
SET 423 0
SET 234 0
SET 634 0
SET 437 0
awk '{print "SET", $1, "0"}' file

This sed with inline switch -i works: 这与内联开关-i工作:

sed -i.bak 's/\([0-9]*\)/SET \1 0/' file

-i.bak will make sure to save changes into the file with backup created with .bak extension. -i.bak将确保使用以.bak扩展名创建的备份将更改保存到文件中。

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

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