如何打开文件并根据第一个文件的内容创建新文件

问题描述 投票:0回答:2

我有一个输入文件original.txt,内容为

AS1023000404 SA26376 EFadadhkaj ASssjdiw9128129010210 EF939809

这里我想基于First 2 letters of each line创建新文件,即从给定的原始文件中,我应该具有以下内容。

file 1 = AS.txt content: AS1023000404 ASssjdiw9128129010210

File 2 = SA.txt Content: SA26376

File 3 = EF.txt Content: EFadadhkaj EF939809

任何人都可以帮助我如何实现这一目标。

在这里我尝试过的添加Perl代码。

while (<$INFILE>) { if (length($_) > 0) { $outFlName = substr($_,$start,$len);

` if (not $OUTFILE{$outFlName}) {
     open $OUTFILE{$outFlName}, '>', "${outFlName}.txt"
       or die "Unable to open '${outFlName}.txt' for output: $!";
     $OUTREC{$outFlName} = 0;
 }
 print { $OUTFILE{$outFlName} } $_;
 $OUTREC{$outFlName} = $OUTREC{$outFlName} + 1;`

} } close $_ for values %OUTFILE;

shell unix awk
2个回答
2
投票

您能不能尝试以下操作。

awk '
{
  output_file=substr($0,1,2)".txt"
}
{
  print >> (output_file)
  close(output_file)
}
' Input_file

说明:添加以上详细说明。

awk '                                   ##Starting awk program from here.
{
  output_file=substr($0,1,2)".txt"      ##Creating output_file which has first 2 letters of current line.
}
{
  print >> (output_file)                ##Printing line to output file.
  close(output_file)                    ##Closing output file in back ground.
}
' file

0
投票

使用GNU awk处理许多同时打开的文件:

awk '{print > (substr($0,1,2) ".txt")}' file

使用任何awk + ​​sort可以不必逐行关闭/打开每个输出文件:

awk '{print NR, substr($0,1,2), $0}' file |
sort -k2,2 -k1,1n |
awk '$2 != prev{close(out); out=$2 ".txt"; prev=$2} {sub(/([^[:space:]]+[[:space:]]+){2},""); print > out}'
© www.soinside.com 2019 - 2024. All rights reserved.