试了下貌似>也可以追加(不会覆盖)到文件,纠结啊,求高人指点
cat a.txt
a|1
b|2
c|3
d|4
awk -F"|" '{if($1=="a"||$1=="b"){$2=$1;print $0 > "a.out"}else{print $0 > "a.out"}}' a.txt
cat a.out
a a
b b
c 3
d 4
awk -F"|" '{if($1=="a"||$1=="b"){$2=$1;print $0 >> "a.out"}else{print $0 >> "a.out"}}' a.txt
cat a.out
a a
b b
c 3
d 4
没有差别?
print items > output-file
This redirection prints the items into the output file named output-file. The file name output-file can be any expression. Its value is changed to a string and then used as a file name (see Expressions).
When this type of redirection is used, the output-file is erased before the first output is written to it. Subsequent writes to the same output-file do not erase output-file, but append to it. (This is different from how you use redirections in shell scripts.) If output-file does not exist, it is created. For example, here is how an awk program can write a list of peoples’ names to one file named name-list, and a list of phone numbers to another file named phone-list:
$awk '{ print $2 > "phone-list">print $1 > "name-list" }' mail-list$cat phone-list-| 555-5553Each output file contains one name or number per line.
print items >> output-file
This redirection prints the items into the preexisting output file named output-file. The difference between this and the single-‘>’ redirection is that the old contents (if any) of output-file are not erased. Instead, the awk output is appended to the file. If output-file does not exist, then it is created.