Awk 搜索图案并每次打印到特定图案上方
如果我有以下格式的数据Awk 搜索图案并每次打印到特定图案上方,awk,sed,Awk,Sed,如果我有以下格式的数据 cell D type seq Cd Input Cp Input Q Output Func IQ Sdn Input cell 23 ---- 我希望输出为单元格、输入、输出、函数、类型格式 cell D Cd Input Cp Input Sdn Input Q Output Func IQ type seq cell 23 --- 即Func和type每次都应出现在单元格前面,即最后一个单元格。 我不知道该怎么做。 在上面的输出中,我希望当输入中有type和
cell D
type seq
Cd
Input
Cp
Input
Q
Output
Func IQ
Sdn
Input
cell 23
----
我希望输出为单元格、输入、输出、函数、类型格式
cell D
Cd
Input
Cp
Input
Sdn
Input
Q
Output
Func IQ
type seq
cell 23
---
即Func和type
每次都应出现在单元格前面,即最后一个单元格。
我不知道该怎么做。
在上面的输出中,我希望当输入中有type和funct时,我希望它最终打印在单元格线上方for cell 23表示此单元格的数据,其格式与cell D格式相同。根据显示的仅用GNU
awk
编写的示例,请尝试以下内容。这是考虑到单元格
每次都以块的形式出现,如果单元格
不是在输入文件的最后一次出现,那么这些值将不会被打印(您可以在结束
块中打印它们,以防您也需要这些值,留作OP练习)
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
/^cell/{ ##Checking if a line starts from cell then do following.
if(funcVal){ ##Checking if funcVal is NOT NULL then do following.
print funcVal ##printing funcVal here.
}
if(typeVal){ ##Checking if typeVal is NOT NULL then do following.
print typeVal ##Printing typeVal value here.
}
funcVal=typeVal="" ##Nullifying values here.
}
/^Func/{ ##Checking if line starts from Func then do following.
funcVal=(funcVal?funcVal ORS:"")$0 ##Creating funcVal and keep adding value of current line to it.
next ##next will skip all further statements from here.
}
/^type/{ ##Checking if line starts from type then do following.
typeVal=(typeVal?typeVal ORS:"")$0 ##Creating typeVal and keep adding value of current line to it.
next ##next will skip all further statements from here.
}
1 ##Mentioning 1 will print current line.
' Input_file ##Mentioning Input_file name here.
由于您的问题到目前为止还不清楚,请您更清楚地解释获得预期输出的逻辑,请仅在您的问题中添加说明。
awk ' ##Starting awk program from here.
/^cell/{ ##Checking if a line starts from cell then do following.
if(funcVal){ ##Checking if funcVal is NOT NULL then do following.
print funcVal ##printing funcVal here.
}
if(typeVal){ ##Checking if typeVal is NOT NULL then do following.
print typeVal ##Printing typeVal value here.
}
funcVal=typeVal="" ##Nullifying values here.
}
/^Func/{ ##Checking if line starts from Func then do following.
funcVal=(funcVal?funcVal ORS:"")$0 ##Creating funcVal and keep adding value of current line to it.
next ##next will skip all further statements from here.
}
/^type/{ ##Checking if line starts from type then do following.
typeVal=(typeVal?typeVal ORS:"")$0 ##Creating typeVal and keep adding value of current line to it.
next ##next will skip all further statements from here.
}
1 ##Mentioning 1 will print current line.
' Input_file ##Mentioning Input_file name here.