将文本文件目录组合成CSV,每行一个文件

时间:2013-06-25 19:33:46

标签: bash

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

说file1.txt:

  

不依赖于相信的倾听。足够周围移除到   巴顿对此表示遗憾。可以估计优势   规定。今年拍得很好,现在已经过了。应该站起来了   嫁给了他。做相关的先生账户品牌。错误   从来没有准备好火腿这些诙谐的他。我们的指南针看到年龄不文明   事情天气禁止她的分钟。准备如何但真相儿子新   下。

     

在对比增长的情况下,他认为是令人惊讶的。如   如果被拍成一团。通过它足以让山谷渴望做到。太太太太   伟大的女仆这些火腿匹配她。 Abode尝试做的事情   女佣。令人怀疑的处置返回欢欣到达斯特伍德是如此。

和file2.txt:

  

在去的庄园里做过。你们是否因为同情而受到赞美   考虑。五月欣喜若狂,确实让这个无知的年龄感到惊喜。拥有   她最后小姐冷。如果他的处理时间过长,那就太多了。怎么但是   儿子夫人什么时候。她尤其不愉快   继续毫无保留的决议。因此希望中国充满希望   和。它是否认为楼梯分支可以承受三十分钟。

     

盲人会相等,而你先做风格。 Lain领导,事实没有。一   优先运动员继续解决幸福感。高中   大声丰富真实。哦传达他的直接尖锐。相等   欢迎她设置没有任何重力是否派对。肥沃的假设   羞怯先生指出要保持尊重。

我需要做的是创建一个新文件,比如说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中找到这方面的实现。

8 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:4)

这是一个纯粹的INTERCAL实现,不需要bashtrcat

        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

答案 3 :(得分:3)

使用awk

awk 'FILENAME!=f&&NR>1{print "\n"}{FILENAME=f}1' ORS='' file1.txt file2.txt > allfiles.txt

答案 4 :(得分:3)

组合Perl / bash解决方案:

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

仅Perl解决方案

#!/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";
}

答案 5 :(得分:1)

这是一个纯粹的解决方案:没有cattrawk等等......

此外,它将具有良好的输出格式:您将不会像其他答案中提供的方法那样获得双倍空格,或开始或尾随空格。

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 来实现你想要的(但这次它不是一个纯粹的解决方案):同样的,这是一个有趣的没有 - 使用报价!

cat

如果您只有两个文件,例如 file1.txt file2.txt ,则可以不使用循环和单个# 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 (和上面相同的警告)和纯

{{1}}

注意。我添加了注释,指出没有引号,因为每个bash程序员在阅读这些不带引号的部分时应该感到不舒服!

注2。你能做得更短吗?

答案 6 :(得分:1)

这可能对您有用:

for file in *.txt ;do paste -s "$file"; done | sed 's/^ *//;s/  */ /g'

答案 7 :(得分:1)

awk '
    FNR == 1 && FILENAME != ARGV[1] {print "\n"}
    {printf "%s",$0}
    END {print ""}
' *.txt > allfiles.txt
相关问题