将列打印到多行 awk

print column to many row awk

我在这个问题上卡了很久。我有一个包含很多行的输入文件

<594711>
<name>
<duyduy>
<SV>
<RUN>
<AL_Ptime>
</common/sAS>
<lsf_login07>
</shsv/DASA>
<594712>
<name>
<thanhthanh>
<SV>
<RUN>
<AL_Ptime>
</common/NDWQ>
<lsf_login07>
</shsv/CXZC>

我希望它像这样把它分成一行。它在作业 ID

之后重复
<594711> <RUN> <AL_Ptime> <lsf_login07> </shsv/DASA>
<594712> <RUN> <AL_Ptime> <lsf_login07> </shsv/CXZC>

以下 awk 代码可能会对您有所帮助。

awk '
/<\/shsv\/[a-zA-Z]+>/{
  val=val?val OFS [=10=]:[=10=];
  print val;
  flag="";
  next
}
/<[0-9]+>/{
  flag=1;
  val=[=10=];
  next
}
val && (([=10=] ~ /<RUN>/) || ([=10=] ~ /<AL_Ptime>/) || ([=10=] ~ /<lsf_login07>/)){
  val=val?val OFS [=10=]:[=10=]
}
'   Input_file

输出如下。

<594711> <RUN> <AL_Ptime> <lsf_login07> </shsv/DASA>
<594712> <RUN> <AL_Ptime> <lsf_login07> </shsv/CXZC>

编辑: 此处也添加代码说明。

awk '
/<\/shsv\/[a-zA-Z]+>/{   ##Search for string shsv with alphabets till their group in a line, then do following.
  val=val?val OFS [=12=]:[=12=]; ##create variable named val whose value is current line when val is NULL else it will concatenating its own value with each line.
  print val;             ##printing variable val here.
  flag="";               ##making variable flag as NULL here.
  next                   ##Using next will skip all statements further.
}
/<[0-9]+>/{              ##Search <digits till group and if it is TRUE then do following.
  flag=1;                ##Setting variable flag to 1 now.
  val=[=12=];                ##making variable val as current line.
  next                   ##Using next will skip all statements further.
}
val && (([=12=] ~ /<RUN>/) || ([=12=] ~ /<AL_Ptime>/) || ([=12=] ~ /<lsf_login07>/)){ ##Checking if variable val is NOT NULL AND (either current line is string <RUN> OR <AL_Ptime> OR <lsf_login07>) then do following.
  val=val?val OFS [=12=]:[=12=]  ##create variable named val whose value is value of current line if va; is NULL else it will concatenate its own value with current line.
}
' Input_file            ##Mentioning the Input_file name here.