如何根据第1列的值将多列合并为两列?

拉卡特拉加

我有以下示例格式的文本文件

File.txt

Record1 20 23;
Record2 256;
Record3 45 679 98 1;

每行中的列数可以变化。我想基于列1值仅用两列更改文件。以下是目标输出。

File_2_Columns.txt

Record1 20;
Record1 23;
Record2 256;
Record3 45;
Record3 679;
Record3 98;
Record3 1;
史蒂夫

这是一种方法

awk '{for(a=2;a<=NF;a++){printf "%s %s%c\n",$1,$a,a==NF ? "" : ";"}}' File.txt >File_2_Columns.txt

祝您在其他作业中好运;-)

如果老师给了最短答案的额外学分,请尝试

awk '{for(a=2;a<=NF;){printf"%s%c\n",$1" "$a,a++-NF?";":""}}' File.txt >File_2_Columns.txt

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章