use*_*178 70 unix linux awk split
我有一个文件-|在每个部分后都带有分隔符...需要使用unix为每个部分创建单独的文件.
输入文件的示例
wertretr
ewretrtret
1212132323
000232
-|
ereteertetet
232434234
erewesdfsfsfs
0234342343
-|
jdhg3875jdfsgfd
sjdhfdbfjds
347674657435
-|
Run Code Online (Sandbox Code Playgroud)
文件1中的预期结果
wertretr
ewretrtret
1212132323
000232
-|
Run Code Online (Sandbox Code Playgroud)
文件2中的预期结果
ereteertetet
232434234
erewesdfsfsfs
0234342343
-|
Run Code Online (Sandbox Code Playgroud)
文件3中的预期结果
jdhg3875jdfsgfd
sjdhfdbfjds
347674657435
-|
Run Code Online (Sandbox Code Playgroud)
ctr*_*lor 82
一个班轮,没有编程.(正则表达式除外)
csplit --digits=2 --quiet --prefix=outfile infile "/-|/+1" "{*}"
Run Code Online (Sandbox Code Playgroud)
Wil*_*ell 34
awk '{print $0 " -|"> "file" NR}' RS='-\\|' input-file
Run Code Online (Sandbox Code Playgroud)
解释(编辑):
RS是记录分隔符,此解决方案使用gnu awk扩展名,允许它是多个字符.NR是记录号码.
print语句打印一条记录,然后打印" -|"到包含其名称中的记录号的文件中.
Debian有csplit,但我不知道这对所有/大多数/其他发行版是否共同.如果没有,那么追踪源代码并编译它应该不会太难......
我解决了一个稍微不同的问题,其中文件包含一个带有名称的行,其中后面的文本应该去.这个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 "$_"};
}
Run Code Online (Sandbox Code Playgroud)