逐行交织两个文本文件

时间:2015-04-05 05:05:54

标签: bash

我有两个文件。 一个文件的名称带有空格,另一个文件的空格已被强大且强大的tr命令替换为下划线。

cat /tmp/crap2
Effective awk Programming, 3rd Edition.pdf
Fashion Photography by Edward Steichen in the 1920s and 1930s (15).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (19).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (30).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (4).jpg
sed & awk, 2nd Edition.pdf


cat /tmp/crap
Effective_awk_Programming,_3rd_Edition.pdf
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
sed_&_awk,_2nd_Edition.pdf

我想将这样的输出显示到文件makinng显示一个带下栏而另一个没有下划线:

Effective_awk_Programming,_3rd_Edition.pdf
Effective awk Programming, 3rd Edition.pdf
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (15).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (19).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (30).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
Fashion Photography by Edward Steichen in the 1920s and 1930s (4).jpg
sed_&_awk,_2nd_Edition.pdf
sed & awk, 2nd Edition.pdf

但是当我使用while循环和嵌套for循环的bash命令时,输出会变得很乱:

$ while read line ; do  for i in $(cat /tmp/crap); do  echo $i ; done ; echo $line ; done < /tmp/crap2


Effective_awk_Programming,_3rd_Edition.pdf
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
sed_&amp;_awk,_2nd_Edition.pdf
Fashion Photography by Edward Steichen in the 1920s and 1930s (30).jpg
cat /tmp/crap)
cat /tmp/crap
Effective_awk_Programming,_3rd_Edition.pdf
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg   
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg 
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
sed_&amp;_awk,_2nd_Edition.pdf
Fashion Photography by Edward Steichen in the 1920s and 1930s (4).jpg
cat /tmp/crap)
cat /tmp/crap
Effective_awk_Programming,_3rd_Edition.pdf
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(15).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(19).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(30).jpg
Fashion_Photography_by_Edward_Steichen_in_the_1920s_and_1930s_(4).jpg
sed_&amp;_awk,_2nd_Edition.pdf
sed &amp; awk, 2nd Edition.pdf

我尝试过更改IFS的其他脚本,然后立即制作不同的cat文件

paste /tmp/crap /tmp/crap2 

两个文件并使用vi进行编辑

2 个答案:

答案 0 :(得分:3)

我相信paste-d,--delimiter选项。所以就这样做

paste -d '\n' /tmp/crap /tmp/crap2

假设cygwin paste类似于coreutils paste或BSD paste

答案 1 :(得分:1)

使用2个文件描述符:

while read -u3 c1 && read -u4 c2; do 
    echo "$c1"
    echo "$c2"
done 3<crap 4<crap2