# 1.0
#=GF ID 45651
#=GF AC CD7.8我使用的命令是
awk '{print $0 > $2 NR}' RS='//' assignment.txt这是生成名为file0、file1等的文件。
发布于 2018-11-20 12:27:52
使用GNU awk进行多字符RS和打开文件处理:
awk -v RS='\n//\n' -F'\n' '{print > ($3 ".txt")}' file这当然会产生糟糕的输出文件名,因为它们将同时包含空格和=符号,并以#开头,但这正是您所要求的.
发布于 2018-11-20 10:13:49
考虑到您不想在输出文件中使用//,请您尝试以下操作。
awk '
/^\/\//{
close(file".txt")
flag=val=""
}
/#=GF AC PF.*/{
flag=1
file=$0
sub(/^#=/,"",file)
print val ORS $0 > file".txt"
next
}
flag{
print > file".txt"
}
!flag{
val=val?val ORS $0:$0
}' Input_file上面的代码将从文件名中删除#=,以防您在输出文件名中也需要这样做,然后也从上面的代码中删除sub(/^#=/,"",file)语句。
解释:现在也添加了对上述代码的解释。
awk ' ##awk program starts here.
/^\/\//{ ##Checking condition if a line has // then do following.
close(file".txt") ##Using close command to close file with file named file".txt" here to avoid TOO MANY FILES OPENED.
flag=val="" ##Nullifying variables flag and val here.
}
/#=GF AC PF.*/{ ##Checking condition if a line starts from #=GF AC PF .* will match anything here, if yes then do following.
flag=1 ##Setting variable flag as 1 here.
file=$0 ##Setting variable file value to current line here.
sub(/^#=/,"",file) ##Substituting #= from start of the line with variable file.
print val ORS $0 > file".txt" ##Printing variable val ORS(output record separator) current line and printing it to file".txt"
next ##next will skip all further statements from here onward.
}
flag{ ##Checking condition if variable flag is SET then do following.
print > file".txt" ##Printing current lines to file".txt" file name.
}
!flag{ ##Checking condition here is variable flag is NOT SET then do following.
val=val?val ORS $0:$0 ##Creating variable val and concatenating to its own value.
}' Input_file ##Mentioning Input_file name here.https://stackoverflow.com/questions/53390613
复制相似问题