Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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-awk/sed向每个字符串添加字符串列表_Bash_Awk_Sed_Cygwin - Fatal编程技术网

bash-awk/sed向每个字符串添加字符串列表

bash-awk/sed向每个字符串添加字符串列表,bash,awk,sed,cygwin,Bash,Awk,Sed,Cygwin,好的,我想将一个文件中的字符串列表添加到另一个文件中的每个字符串中,例如- File1.txt(输入): File2.txt(输入2): expected output.txt: Rexampleasa Rexampleexe Rexampledada Rexample123 Rexample3456 Rexamplenani Rexampleexample Rexamplelol Exampleasa Exampleexe Exampledada Example123 Example3456

好的,我想将一个文件中的字符串列表添加到另一个文件中的每个字符串中,例如-

File1.txt(输入):

File2.txt(输入2):

expected output.txt:

Rexampleasa
Rexampleexe
Rexampledada
Rexample123
Rexample3456
Rexamplenani
Rexampleexample
Rexamplelol
Exampleasa
Exampleexe
Exampledada
Example123
Example3456
Examplenani
Exampleexample
Examplelol

您可以使用
cat
并在每一行上进行迭代:

cat File2.txt |读取t2时;读取t1时执行cat File1.txt |;做回显“${t2}${t1}”;完成;完成

但是,根据File2.txt中的行数,这可能会导致效率低下,因为最终会多次打开File1.txt

使用
awk
可能会更快:

awk'NR==FNR{a[$0];next}{for(a中的i)print i”“$0}”File2.txt File1.txt

我相信有数百种不同的方法可以做到这一点,但选择正确的方法取决于每个文件中的记录数

对于超大数据集,一个有点老套但速度极快的解决方案是:

join-j 9999-t'-o 1.1,2.1 File2.txt File1.txt


最后一篇我摘自@legolegs answer to

@EdMorton我是bash的新手,所以我很难创造任何理论上的可能性XD抱歉,我知道在没有任何概念的情况下提问会被人看不起,但不幸的是,我无法创造。有时很难知道该问什么。在您的例子中,您要求两个文本文件的笛卡尔乘积。这已经在S.O.上被问过了,这里有一个我认为对你有用的答案:谢谢awk版本更适合我!非常感谢你的帮助@rangrunera checkout my last edit,如果你的数据集真的很大,你可以使用join,它可能是fasterBTW,
read
命令用于解析少量键盘输入,使用它来解析文件通常是个坏主意,除非文件很小。这是因为它一次执行一个字节的无缓冲读取,所以每个字节都需要一个系统调用。有关更多详细信息,请参阅。
Rexample
Example
Rexampleasa
Rexampleexe
Rexampledada
Rexample123
Rexample3456
Rexamplenani
Rexampleexample
Rexamplelol
Exampleasa
Exampleexe
Exampledada
Example123
Example3456
Examplenani
Exampleexample
Examplelol