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
没有差别? 展开
[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
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.