Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 同一文件中两列的串联_Bash_Shell_Awk - Fatal编程技术网

Bash 同一文件中两列的串联

Bash 同一文件中两列的串联,bash,shell,awk,Bash,Shell,Awk,从文本文件 文件 a d b e c f 以制表符分隔的列如何连接到一列中 现在我使用awk将列输出到两个文件中,然后使用cat将它们连接起来。但是必须有更好的单行命令吗?这个简单的awk命令应该可以完成以下任务: awk '{print $1; s=s $2 ORS} END{printf "%s", s}' file a b c d e f 这个简单的awk命令应该完成以下工作: awk '{print $1; s=s $2 ORS} END{printf "%s",

从文本文件

文件

a    d
b    e
c    f
以制表符分隔的列如何连接到一列中


现在我使用awk将列输出到两个文件中,然后使用cat将它们连接起来。但是必须有更好的单行命令吗?

这个简单的awk命令应该可以完成以下任务:

awk '{print $1; s=s $2 ORS} END{printf "%s", s}' file
a
b
c
d
e
f

这个简单的awk命令应该完成以下工作:

awk '{print $1; s=s $2 ORS} END{printf "%s", s}' file
a
b
c
d
e
f

您可以使用流程替换;这样就不需要为每个列创建文件

$ 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
$ cat <(awk '{print $1}' file) <(awk '{print $2}' file)
a
b
c
d
e
f
$ 

如果您使用的是Notepad++,则可以用换行符“\r\n”

替换所有选项卡值。如果您使用的是Notepad++,则可以用换行符“\r\n”

替换所有选项卡值。另一种方法:

for i in $(seq 1 2); do
    awk '{print $'$i'}' file
done
输出:

a
b
c
d
e
f
另一种方法:

for i in $(seq 1 2); do
    awk '{print $'$i'}' file
done
输出:

a
b
c
d
e
f

对于广义方法

$ f() { awk '{print $'$1'}' file; }; f 1; f 2

a
b
c
d
e
f
如果文件以制表符分隔,可能只是使用
cut
(与
paste
相反的操作)


对于广义方法

$ f() { awk '{print $'$1'}' file; }; f 1; f 2

a
b
c
d
e
f
如果文件以制表符分隔,可能只是使用
cut
(与
paste
相反的操作)


try:不需要读取文件两次或任何其他命令的外部调用,只需要一个awk来拯救。还要考虑到您的输入文件与所示示例相同

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的结尾部分,打印VAL1和VAL2的值

try:在不读取文件两次或不调用任何其他命令的情况下,只需使用一个awk进行救援。还要考虑到您的输入文件与所示示例相同

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的结尾部分,打印VAL1和VAL2的值

您可以将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

$whileread-r行;做回显“行:$line”;完成<您可以将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


$whileread-r行;做回显“行:$line”;done<他询问关于使用bash脚本(参见标记)。此外,这将交错列。他询问关于使用bash脚本(参见标记)。此外,这将交错列。在这种情况下,
cat
看起来没用:
(awk'{print$1}文件;awk'{print$2}文件)
应该足够了。子shell也不需要,只是
awk。。;awk..
就可以了。在这种情况下,
cat
看起来没用:
(awk'{print$1}'文件;awk'{print$2}'文件)
应该足够了。子shell也不需要,只要
awk。。;awk..
可以。IMHO,我认为OP要求的输出与上面不同,因为OP需要先打印1美元,然后打印2美元。谢谢@RavinderSingh13,我忽略了这一部分。现在更正了。IMHO,我认为OP要求的输出与上面不同,因为OP需要先打印1美元,然后打印2美元。谢谢@RavinderSingh13,我忽略了这一部分。现在已更正。这仅在输入碰巧被排序时有效,但在一般情况下不起作用。它交错然后排序。假设输入看起来像
a b\nc d
。所需的输出为
a\nc\nb\nd
,但您的输出为
a\nb\nc\nd
。分隔符插入在$1和$2之间,LF(\n)不是一个文字值,具体取决于插入方式。与其他方法相比,这确实是一种非常快速的方法。可能因为它每行只读一次。但如前所述,它不连接列。但它可能非常有用。请将答案还原为0,尽管您更喜欢其他方法,但这仅在输入碰巧被排序时有效,但在一般情况下不起作用。它会交错然后排序。假设输入看起来像
a b\nc d
。所需的输出为
a\nc\nb\nd
,但您的输出为
a\nb\nc\nd
。分隔符插入在$1和$2之间,LF(\n)不是一个文字值,具体取决于插入方式。与其他方法相比,这确实是一种非常快速的方法。可能因为它每行只读一次。但如前所述,它不连接列。但它可能非常有用。请将答案还原为0,即使您更喜欢其他方法,但cut方法似乎是连接文件的最快方法。我将在一个有6400万行的文本文件上运行它,所以速度是受欢迎的。我将在一个有6400万行的文本文件上运行它,所以速度是受欢迎的。