So I have a file list.txt similar to this one:
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?
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. 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:
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 '$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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.