# 1.0
#=GF ID 45651
#=GF AC CD7.8
我使用的命令是
awk '{print $0 > $2 NR}' RS='//' assignment.txt
这将生成名为file0,file1等的文件。
答案 0 :(得分:2)
使用GNU awk进行多字符RS和打开文件处理:
No Tool_Name Tool_Part
-- --------- ---------
43 36 3 29. 1
50 36 3 15.9 1
64 2 1 6. 1
72 10 1 9. 1
73 10 1 5.3 1
75 3 1 5.5 1
76 3 1 5.3 1
102 34 1 1.7 1
251 2 3 3. 1
600 33 1 3. 1
这当然会产生可怕的输出文件名,因为它们同时包含空格和awk -v RS='\n//\n' -F'\n' '{print > ($3 ".txt")}' file
,并以=
开头,但这就是您要的...
答案 1 :(得分:1)
考虑到您不希望在输出文件中使用//
,请尝试以下操作。
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.