1

我有一个很大的文本文件目录,每个都相当复杂:

说file1.txt:

我不是在听,取决于相信。够绕移到巴顿同意后悔中还是吧。优势先生估计被指挥规定。当年的好镜头否认现在出现了。应唐斯的立场嫁给了他。做相关的先生帐户布兰登。这些机智的他从来没有准备好火腿,这是错误的。我们的指南针看到年龄不文明的事情天气禁止她分钟。准备好怎么可是真子新下。

与他认为吃惊的情况相比,我的表现越来越好。仿佛被击中了一样。由它足以谷欲尽。大婶婶这些都是火腿配她。住所尝试做事的女仆。疑虑重重的回过神来,达斯伍德就是这么起来的。

和file2.txt:

在去庄园谁做了。你是否庆祝它同情考虑。可欣喜若狂的优雅确实让懵懂的年纪大吃一惊。拥有她的小姐冷最后。如果他的寿命超过处置,那就太多了。怎么可是儿子夫人夫人什么时候。她尤其是不愉快的改变继续毫无保留的解决。因此希望嘈杂的中国能充分和。是不是把楼梯支三十长负担得起。

盲人将平等,而哦,先生做风格。莱恩领导,事实上没有。一位优选的运动员解决了幸福继续。高处的中响博真。哦,在他传达着立即的敏锐度。同样欢迎她的设定,无论当事人是否有份量。肥沃假设害羞先生指出要保持尊重。

我需要做的是创建一个新文件,比如 allfiles.txt,即:

Am no an listening depending up believing. Enough around remove to barton agreed regret in or it. Advantage mr estimable be commanded provision. Year well shot deny shew come now had. Shall downs stand marry taken his for out. Do related mr account brandon an up. Wrong for never ready ham these witty him. Our compass see age uncivil matters weather forbade her minutes. Ready how but truth son new under. Am increasing at contrasted in favourable he considered astonished. As if made held in an shot. By it enough to valley desire do. Mrs chief great maids these which are ham match she. Abode to tried do thing maids. Doubtful disposed returned rejoiced to dashwood is so up. 

Among going manor who did. Do ye is celebrated it sympathize considered. May ecstatic did surprise elegance the ignorant age. Own her miss cold last. It so numerous if he outlived disposal. How but sons mrs lady when. Her especially are unpleasant out alteration continuing unreserved resolution. Hence hopes noisy may china fully and. Am it regard stairs branch thirty length afford. Blind would equal while oh mr do style. Lain led and fact none. One preferred sportsmen resolving the happiness continued. High at of in loud rich true. Oh conveying do immediate acuteness in he. Equally welcome her set nothing has gravity whether parties. Fertile suppose shyness mr up pointed in staying on respect. 

在这种情况下,这个文件只有两行,每行都是全文。我已经搜索了档案,但似乎无法在 bash 中找到它的实现。

4

8 回答 8

5
for file in dir/* #Process all files in directory
do
   tr '\n' ' ' < "$file" # Remove newlines
   echo ''   # Add newline between files
done > newfile # Write all the output of the loop to the newfile
于 2013-06-25T19:41:10.983 回答
5
touch allfiles.txt # create allfiles.txt
for f in *.txt; do # for each file of the current directory
    cat "$f" | tr '\n' ' ' >> allfiles.txt; # append the content of that file to allfiles.txt
    echo >> allfiles.txt # insert a new line
done
于 2013-06-25T19:41:19.703 回答
4

这是一个纯 INTERCAL 实现,没有bash, tr, 或cat必需:

        PLEASE DO ,1 <- #1
        DO .4 <- #0
        DO .5 <- #0
        DO COME FROM (30)
        PLEASE ABSTAIN FROM (40)
        DO WRITE IN ,1
        DO .1 <- ,1SUB#1
        DO (10) NEXT
        PLEASE GIVE UP
(20)    PLEASE RESUME '?.1$#256'~'#256$#256'
(10)    DO (20) NEXT
        DO FORGET #1
        PLEASE DO .2 <- .4
        DO (1000) NEXT
        DO .4 <- .3~#255
        PLEASE DO .3 <- !3~#15'$!3~#240'
        DO .3 <- !3~#15'$!3~#240'
        DO .2 <- !3~#15'$!3~#240'
        PLEASE DO .1 <- .5
        DO (1010) NEXT
        DO .5 <- .2
        DO ,1SUB#1 <- .3
(30)    PLEASE READ OUT ,1
        PLEASE NOTE: having had pressing business at the local pub
(40)    the author got bored with this implementation
于 2013-06-25T23:18:02.040 回答
3

awk

awk 'FILENAME!=f&&NR>1{print "\n"}{FILENAME=f}1' ORS='' file1.txt file2.txt > allfiles.txt
于 2013-06-25T20:09:40.870 回答
3

Combined Perl/bash solution:

for f in *.txt; do 
  perl -ne 'chomp; print "$_ "; END{ print "\n" }' "$f"
done > output.txt

Perl-only solution

#!/usr/bin/env perl

use strict;
use warnings;

foreach my $file (<*.txt>) {
  open FILE, "<$file" or die $!;
  while (<FILE>) {
    chomp;
    print "$_ ";
  }
  close FILE;
  print "\n";
}
于 2013-06-25T20:12:19.430 回答
1
awk '
    FNR == 1 && FILENAME != ARGV[1] {print "\n"}
    {printf "%s",$0}
    END {print ""}
' *.txt > allfiles.txt
于 2013-06-25T23:44:37.447 回答
1

这是一个纯粹的解决方案: no cat, tr, awk, 等等...

此外,它将有一个很好的输出格式:你不会像其他答案中提供的方法那样得到双空格,或者开始或尾随空格。

for f in *.txt; do
    # There are purposely no quotes for $(<"$f")
    echo $(<"$f")
    echo
done > newfile

唯一需要注意的是,如果文件以-e,-E-n: 开头,则不会输出这些字符:echo考虑到这是一个选项,它们会被吞掉。但我想这不太可能发生!

诀窍是echo $l不带引号使用!


使用这个技巧,你可以用cat一种有趣的方式来实现你想要的(但这次它不是一个纯粹的解决方案):同样的事情,它是一个有趣的不使用引号!

for f in *.txt; do
    # There are purposely no quotes for $(<"$f")
    cat <<< $(<"$f")
    echo
done > newfile

如果你只有两个文件,比如file1.txtfile2.txt,你可以不用循环和一个cat命令:

# there's purposely a lack of quotes
cat <<< $(<file1.txt)$'\n\n'$(<file2.txt) > newfile

或使用单个echo(与上述相同的警告)和纯

# there's purposely a lack of quotes
echo $(<file1.txt)$'\n\n'$(<file2.txt) > newfile

笔记。我添加了注释以指定没有引号,因为每个 bash 程序员在阅读这些未引用的部分时都会感到不舒服!

笔记2。你能做的更短吗?

于 2013-06-25T21:32:51.657 回答
1

这可能对您有用:

for file in *.txt ;do paste -s "$file"; done | sed 's/^ *//;s/  */ /g'
于 2013-06-25T23:27:33.517 回答