shell - 如何打开文件并从第一个文件的内容创建新文件
问题描述
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;
解决方案
请您尝试以下操作。
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
推荐阅读
- azure - KQL 时间图 漏洞数量
- javascript - 计算两个日期时间对象或字符串之间的天数的最佳方法是什么?
- azure - Azure 应用服务身份验证不允许个人帐户
- reactjs - NextJS 如何在导航栏中调用动态内容
- biztalk - 将具有重复节点输入的平面文件架构映射到具有重复节点输出的一个 XML 架构 BizTalk
- .htaccess - 子域重定向到子文件夹工作正常,但不适用于页面
- python - 如何使 LSTM 网络翻译不变/等变?
- node.js - 使用 node.js 在 Firebase 存储中的文件夹中上传文件
- cobol - 如何修复 RPT 文件中的重复字段?
- java - 调用片段方法给出空对象引用