awk输出方式,使用>与>>定向到某个文件时有没有差别?

试了下貌似>也可以追加(不会覆盖)到文件,纠结啊,求高人指点
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
没有差别?

awk 里 >> myfile 的意思是如果myfile已然存在, awk的输出不会覆盖myfile原有的内容,而是追加在其后
而 > myfile; 若myfile 已存在,awk输出overwrite原有内容
>>追加而不覆盖, 不是指awk后面输出的行覆盖先前输出的行
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-19
你确定你自己亲自尝试过?!为什么我尝试的就是正常的呢

[root@localhost ~]# awk -F"|" '{if($1=="a"||$1=="b"){$2=$1;print $0 > "a.out"}else{print $0 > "a.out"}}' a.txt
[root@localhost ~]# cat a.tout
cat: a.tout: 没有那个文件或目录
[root@localhost ~]# cat a.out
a a
b b
c|3
d|4
[root@localhost ~]# awk -F"|" '{if($1=="a"||$1=="b"){$2=$1;print $0 >> "a.out"}else{print $0 >> "a.out"}}' a.txt
[root@localhost ~]# cat a.out
a a
b b
c|3
d|4
a a
b b
c|3
d|4
第2个回答  2019-04-28

    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-5553
    -| 555-3412

    $cat name-list-| Amelia
    -| Anthony

    Each 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.