【发布时间】:2017-05-25 14:48:45
【问题描述】:
来自文本文件
文件
a d b e c f
制表符分隔的列如何连接成一列
a b c d e f
现在我使用 awk 将列输出到两个文件,然后我使用 cat 将它们连接起来。但是一定有更好的单行命令?
【问题讨论】:
来自文本文件
文件
a d b e c f
制表符分隔的列如何连接成一列
a b c d e f
现在我使用 awk 将列输出到两个文件,然后我使用 cat 将它们连接起来。但是一定有更好的单行命令?
【问题讨论】:
对于广义的方法
$ f() { awk '{print $'$1'}' file; }; f 1; f 2
a
b
c
d
e
f
如果文件是制表符分隔的,也许只是用cut(paste 的逆运算)
$ cut -f1 file.t; cut -f2 file.t
【讨论】:
您可以使用进程替换;这将消除为每列创建文件的需要。
$ cat file
a d
b e
c f
$ cat <(awk '{print $1}' file) <(awk '{print $2}' file)
a
b
c
d
e
f
$
或
根据评论,您可以组合多个命令并将它们的输出重定向到不同的文件,如下所示:
$ cat file
a d
b e
c f
$ (awk '{print $1}' file; awk '{print $2}' file) > output
$ cat output
a
b
c
d
e
f
$
【讨论】:
cat 看起来没用:(awk '{print $1}' file; awk '{print $2}' file) 应该足够了。
awk ..; awk .. 就可以了。
try:无需两次读取文件或没有任何其他命令的任何外部调用,只需单个 awk 即可救援。还考虑到您的 Input_file 与显示的示例相同。
awk '{VAL1=VAL1?VAL1 ORS $1:$1;VAL2=VAL2?VAL2 ORS $2:$2} END{print VAL1 ORS VAL2}' Input_file
解释:只需创建一个名为 VAL1 的变量,该变量将包含 $1 的值并继续连接它自己的值,VAL2 将拥有 $2 的值并继续连接它自己的值。在 awk 的 END 部分打印 VAL1 和 VAL2 的值。
【讨论】:
这个简单的 awk 命令应该可以完成这项工作:
awk '{print $1; s=s $2 ORS} END{printf "%s", s}' file
a
b
c
d
e
f
【讨论】:
您可以将 bash 命令与 ; 结合使用以获取单个流:
$ awk '{print $1}' file; awk '{print $2}' file
a
b
c
d
e
f
如果您希望它是单个文件,请使用进程替换:
$ txt=$(awk '{print $1}' file; awk '{print $2}' file)
$ echo "$txt"
a
b
c
d
e
f
或者对于 Bash while 循环:
$ while read -r line; do echo "line: $line"; done < <(awk '{print $1}' file; awk '{print $2}' file)
line: a
line: b
line: c
line: d
line: e
line: f
【讨论】:
另一种方法:
for i in $(seq 1 2); do
awk '{print $'$i'}' file
done
输出:
a
b
c
d
e
f
【讨论】:
a b\nc d。期望的输出是a\nc\nb\nd,但你的输出是a\nb\nc\nd。