根据分隔符将一个文件拆分为多个文件

时间:2012-07-03 15:07:03

标签: linux unix awk split

我在每个部分之后有一个-|作为分隔符的文件...需要使用unix为每个部分创建单独的文件。

输入文件的示例

wertretr
ewretrtret
1212132323
000232
-|
ereteertetet
232434234
erewesdfsfsfs
0234342343
-|
jdhg3875jdfsgfd
sjdhfdbfjds
347674657435
-|

文件1中的预期结果

wertretr
ewretrtret
1212132323
000232
-|

文件2中的预期结果

ereteertetet
232434234
erewesdfsfsfs
0234342343
-|

文件3中的预期结果

jdhg3875jdfsgfd
sjdhfdbfjds
347674657435
-|

11 个答案:

答案 0 :(得分:79)

一个班轮,没有编程。 (正则表达式等除外)

csplit --digits=2  --quiet --prefix=outfile infile "/-|/+1" "{*}"

答案 1 :(得分:29)

awk '{print $0 " -|"> "file" NR}' RS='-\\|'  input-file

解释(已编辑):

RS是记录分隔符,此解决方案使用gnu awk扩展名,允许它包含多个字符。 NR是记录号。

print语句将记录后跟" -|"打印到包含其名称中记录号的文件中。

答案 2 :(得分:7)

Debian有csplit,但我不知道这对所有/大多数/其他发行版是否共同。但是,如果没有,那么追踪源代码并编译它应该不会太难......

答案 3 :(得分:5)

我解决了一个稍微不同的问题,其中文件包含一个带有名称的行,其中后面的文本应该去。这个perl代码对我有用:

#!/path/to/perl -w

#comment the line below for UNIX systems
use Win32::Clipboard;

# Get command line flags

#print ($#ARGV, "\n");
if($#ARGV == 0) {
    print STDERR "usage: ncsplit.pl --mff -- filename.txt [...] \n\nNote that no space is allowed between the '--' and the related parameter.\n\nThe mff is found on a line followed by a filename.  All of the contents of filename.txt are written to that file until another mff is found.\n";
    exit;
}

# this package sets the ARGV count variable to -1;

use Getopt::Long;
my $mff = "";
GetOptions('mff' => \$mff);

# set a default $mff variable
if ($mff eq "") {$mff = "-#-"};
print ("using file switch=", $mff, "\n\n");

while($_ = shift @ARGV) {
    if(-f "$_") {
    push @filelist, $_;
    } 
}

# Could be more than one file name on the command line, 
# but this version throws away the subsequent ones.

$readfile = $filelist[0];

open SOURCEFILE, "<$readfile" or die "File not found...\n\n";
#print SOURCEFILE;

while (<SOURCEFILE>) {
  /^$mff (.*$)/o;
    $outname = $1;
#   print $outname;
#   print "right is: $1 \n";

if (/^$mff /) {

    open OUTFILE, ">$outname" ;
    print "opened $outname\n";
    }
    else {print OUTFILE "$_"};
  }

答案 4 :(得分:3)

以下命令对我有用。希望它有所帮助。

awk 'BEGIN{file = 0; filename = "output_" file ".txt"}
    /-|/ {getline; file ++; filename = "output_" file ".txt"}
    {print $0 > filename}' input

答案 5 :(得分:2)

您也可以使用awk。我对awk不太熟悉,但以下似乎对我有用。它生成了part1.txt,part2.txt,part3.txt和part4.txt。请注意,生成的最后一个partn.txt文件为空。我不确定如何解决这个问题,但我确信可以通过一些调整来完成。有人建议吗?

awk_pattern文件:

BEGIN{ fn = "part1.txt"; n = 1 }
{
   print > fn
   if (substr($0,1,2) == "-|") {
       close (fn)
       n++
       fn = "part" n ".txt"
   }
}

bash命令:

awk -f awk_pattern input.file

答案 6 :(得分:1)

这是一个Python 3脚本,它根据分隔符提供的文件名将文件拆分为多个文件。示例输入文件:

# Ignored

######## FILTER BEGIN foo.conf
This goes in foo.conf.
######## FILTER END

# Ignored

######## FILTER BEGIN bar.conf
This goes in bar.conf.
######## FILTER END

这是脚本:

#!/usr/bin/env python3

import os
import argparse

# global settings
start_delimiter = '######## FILTER BEGIN'
end_delimiter = '######## FILTER END'

# parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-file", required=True, help="input filename")
parser.add_argument("-o", "--output-dir", required=True, help="output directory")

args = parser.parse_args()

# read the input file
with open(args.input_file, 'r') as input_file:
    input_data = input_file.read()

# iterate through the input data by line
input_lines = input_data.splitlines()
while input_lines:
    # discard lines until the next start delimiter
    while input_lines and not input_lines[0].startswith(start_delimiter):
        input_lines.pop(0)

    # corner case: no delimiter found and no more lines left
    if not input_lines:
        break

    # extract the output filename from the start delimiter
    output_filename = input_lines.pop(0).replace(start_delimiter, "").strip()
    output_path = os.path.join(args.output_dir, output_filename)

    # open the output file
    print("extracting file: {0}".format(output_path))
    with open(output_path, 'w') as output_file:
        # while we have lines left and they don't match the end delimiter
        while input_lines and not input_lines[0].startswith(end_delimiter):
            output_file.write("{0}\n".format(input_lines.pop(0)))

        # remove end delimiter if present
        if not input_lines:
            input_lines.pop(0)

最后,请按照以下方式运行它:

$ python3 script.py -i input-file.txt -o ./output-folder/

答案 7 :(得分:1)

如果有,请使用class DestinationViewController: UIViewController { var delegate: ConversionDelegate! var type: ConversionType! // your usual stuff like viewDidLoad @IBAction func calculateButtonPressed(_ sender: UIButton) { switch type { case .fahrenheitToCelsius: // read the input value from somewhere like a UITextField // most likely you will have to convert a String to Double in the example let result = delegate.convertToCelsius(fromFahrenheitDegrees: yourInputValue) // output the result to some label or whatever you like } } }

如果你没有,但你有Python ......不要使用Perl。

懒惰读取文件

您的文件可能太大而无法一次性保存在内存中 - 逐行读取可能更为可取。假设输入文件名为“samplein”:

csplit

答案 8 :(得分:0)

cat file| ( I=0; echo -n "">file0; while read line; do echo $line >> file$I; if [ "$line" == '-|' ]; then I=$[I+1]; echo -n "" > file$I; fi; done )

和格式化版本:

#!/bin/bash
cat FILE | (
  I=0;
  echo -n"">file0;
  while read line; 
  do
    echo $line >> file$I;
    if [ "$line" == '-|' ];
    then I=$[I+1];
      echo -n "" > file$I;
    fi;
  done;
)

答案 9 :(得分:0)

这是一个将执行该操作的perl代码

#!/usr/bin/perl
open(FI,"file.txt") or die "Input file not found";
$cur=0;
open(FO,">res.$cur.txt") or die "Cannot open output file $cur";
while(<FI>)
{
    print FO $_;
    if(/^-\|/)
    {
        close(FO);
        $cur++;
        open(FO,">res.$cur.txt") or die "Cannot open output file $cur"
    }
}
close(FO);

答案 10 :(得分:-1)

这是我为上下文分割的问题: http://stromberg.dnsalias.org/~strombrg/context-split.html

$ ./context-split -h
usage:
./context-split [-s separator] [-n name] [-z length]
        -s specifies what regex should separate output files
        -n specifies how output files are named (default: numeric
        -z specifies how long numbered filenames (if any) should be
        -i include line containing separator in output files
        operations are always performed on stdin