首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将两个文本文件逐行交织在一起

将两个文本文件逐行交织在一起
EN

Stack Overflow用户
提问于 2015-04-05 05:05:54
回答 2查看 68关注 0票数 1

我有两个文件。一个文件名为空格,另一个文件的空格已被强大而强大的tr命令替换为underbar。

代码语言:javascript
复制
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

我想让输出像这样显示到文件,使之显示--一种是用酒吧显示的,另一种是用非酒吧显示的:

代码语言:javascript
复制
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

但是,当我将bash命令与while循环和嵌套的for循环一起使用时,输出就会非常混乱:

代码语言:javascript
复制
$ 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文件,现在我所做的是

代码语言:javascript
复制
paste /tmp/crap /tmp/crap2 

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-05 13:51:28

使用2个文件描述符:

代码语言:javascript
复制
while read -u3 c1 && read -u4 c2; do 
    echo "$c1"
    echo "$c2"
done 3<crap 4<crap2
票数 1
EN

Stack Overflow用户

发布于 2015-04-05 05:14:44

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

代码语言:javascript
复制
paste -d '\n' /tmp/crap /tmp/crap2

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29454293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档