英文:
linux merge rows if Columns less than X
问题
I need you support in creating the output.
我需要你的支持来创建输出。
I have output as
我的输出如下
1 ABC STATUS
7 PROHIBITED
8 PROHIBITED
2 CDE STATUS
7 PROHIBITED
8 PROHIBITED
3 FGH STATUS
142 PROHIBITED
2 HIJ STATUS
7 PROHIBITED
I am trying to achieve as
我试图实现以下结果
`1 ABC STATUS 7 PROHIBITED 8 PROHIBITED
2 CDE STATUS 7 PROHIBITED 8 PROHIBITED
3 FGH STATUS 142 PROHIBITED
2 HIJ STATUS 7 PROHIBITED
OR Either below where every first and second column is filled in below empty gaps.
或者下面的方式,其中每个第一列和第二列都填在下面的空白处。
1 ABC STATUS 1 ABC 7 PROHIBITED 1 ABC 8 PROHIBITED 2 CDE STATUS 2 CDE 7 PROHIBITED 2 CDE 8 PROHIBITED 3 FGH STATUS 3 FGH 142 PROHIBITED 2 HIJ STATUS 2 HIJ 7 PROHIBITED 2 HIJ 8 PROHIBITED
so far I have tried to achieve this in one single row with
到目前为止,我已经尝试在一行中实现这个结果
cat file | paste -sd'\t\n';
but it is not showing the result as expected. it would be appreciated if any one can provide me with guidance as to how to achieve
但它没有显示预期的结果。如果有人可以提供关于如何实现的指导,将不胜感激。
英文:
I need you support in creating the output.
I have output as
1 ABC STATUS
7 PROHIBITED
8 PROHIBITED
2 CDE STATUS
7 PROHIBITED
8 PROHIBITED
3 FGH STATUS
142 PROHIBITED
2 HIJ STATUS
7 PROHIBITED
I am trying to achieve as
`1 ABC STATUS 7 PROHIBITED 8 PROHIBITED
2 CDE STATUS 7 PROHIBITED 8 PROHIBITED
3 FGH STATUS 142 PROHIBITED
2 HIJ STATUS 7 PROHIBITED
OR Either below where every first and second column is filled in below empty gaps.
`1 ABC STATUS
1 ABC 7 PROHIBITED
1 ABC 8 PROHIBITED
2 CDE STATUS
2 CDE 7 PROHIBITED
2 CDE 8 PROHIBITED
3 FGH STATUS
3 FGH 142 PROHIBITED
2 HIJ STATUS
2 HIJ 7 PROHIBITED
2 HIJ 8 PROHIBITED `
so far I have tried to achieve this in one single row with
cat file | paste -sd'\t\n'
but it is not showing the result as expected. it would be appreciated if any one can provide me with guidance as to how to achieve
答案1
得分: 2
以下是代码的翻译部分:
{ if (NF==3)
printf "%s%s",(NR>1 ? "\n" : ""),$0 # 如果不是输入的第一行,则在输出前加上"\n"(用于终止先前的输出行)
else
printf "%s",$0
}
END {print ""} # 终止最后一行的输出
' file
这段代码的功能是生成以下输出:
1 ABC STATUS 7 PROHIBITED 8 PROHIBITED
2 CDE STATUS 7 PROHIBITED 8 PROHIBITED
3 FGH STATUS 142 PROHIBITED
2 HIJ STATUS 7 PROHIBITED 8 PROHIBITED
英文:
One awk
idea for generating the first set of output:
awk '
{ if (NF==3)
printf "%s%s",(NR>1 ? "\n" : ""),$0 # if not 1st row of input then prefix the output with "\n" (to terminate previous line of output)
else
printf "%s",$0
}
END {print ""} # terminate last line of output
' file
This generates:
1 ABC STATUS 7 PROHIBITED 8 PROHIBITED
2 CDE STATUS 7 PROHIBITED 8 PROHIBITED
3 FGH STATUS 142 PROHIBITED
2 HIJ STATUS 7 PROHIBITED 8 PROHIBITED
答案2
得分: 2
使用任何POSIX awk,您可以测试一行是否以空格开头:
1 ABC STATUS 7 PROHIBITED 8 PROHIBITED
2 CDE STATUS 7 PROHIBITED 8 PROHIBITED
3 FGH STATUS 142 PROHIBITED
2 HIJ STATUS 7 PROHIBITED
英文:
Using any POSIX awk you can just test if a line starts with a space or not:
$ awk '{printf "%s%s", (/^[[:space:]]/ ? "" : sep), $0; sep=ORS} END{print ""}' file
1 ABC STATUS 7 PROHIBITED 8 PROHIBITED
2 CDE STATUS 7 PROHIBITED 8 PROHIBITED
3 FGH STATUS 142 PROHIBITED
2 HIJ STATUS 7 PROHIBITED
答案3
得分: 2
以下是已翻译的代码部分:
awk -v RS='(^|\n)[0-9]+' -v OFS="\t" '
RT && FNR>1{
gsub(/\n/,"")
$1=$1
print
}
END{
if($0){
gsub(/^ +|\n/,"")
$1=$1
print
}
}
' Input_file
英文:
With your shown samples and attempts please try following GNU awk
code.
awk -v RS='(^|\n)[0-9]+' -v OFS="\t" '
RT && FNR>1{
gsub(/\n/,"")
$1=$1
print
}
END{
if($0){
gsub(/^ +|\n/,"")
$1=$1
print
}
}
' Input_file
答案4
得分: 1
如果要合并的行始终以空格开头,如示例输入中所示,那么可以使用 sed
解决方案:
sed -e :a -e '$!N;s/\n\([[:blank:]]\)//;ta' -e 'P;D' file
英文:
If lines to be joined always begin with blanks as in the sample input, then a sed
solution could be
sed -e :a -e '$!N;s/\n\([[:blank:]]\)//;ta' -e 'P;D' file
答案5
得分: 1
这可能适用于你(GNU sed):
sed '/^\S/{:a;x;s/\n//gp;d};H;$!d;ba' 文件名
将记录存储在保持空间,并在新记录的开始或文件末尾时,切换回保持空间,移除任何换行符并打印。
注:新记录以行首的非空白字符为标志。
英文:
This might work for you (GNU sed):
sed '/^\S/{:a;x;s/\n//gp;d};H;$!d;ba' file
Store records in the hold space and at the start of a new record or the end of the file, swap back to the hold space, remove any newlines and print.
N.B. A new record is designated by a non-white space character at the start of a line.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论