0

这可以使用awk实现吗?我希望添加一个新列,该列已从现有列中分隔数据。我的输入文件是一个制表符分隔的文本文件。带有 device-0, device-1 的列是从 phone-0-1, phone-1-2 派生的新列。

输入:

category1    phone-0-1   working     0000   0000     new     0
category1    phone-1-2   working     0000   0000     new     0
category1    phone-2-4   working     0000   0000     new     0
category1    phone-3-5   working     0000   0000     new     0
category1    phone-4-6   working     0000   0000     new     0

输出:

category1    device-0   phone-0-1    working     0000   0000     new     0
category1    device-1   phone-1-2    working     0000   0000     new     0
category1    device-2   phone-2-4    working     0000   0000     new     0
category1    device-3   phone-3-5    working     0000   0000     new     0
category1    device-4   phone-4-6    working     0000   0000     new     0
4

1 回答 1

3

试试这条 sed 线,看看它是否有效:

 sed 's/\sphone-[0-9]\+/&\t&/' file

与 awk 相同的想法:

awk -F'\t' -v OFS='\t' '{sub(/phone-[0-9]+/,"&\t&",$2)}7' file

编辑,重命名为设备:

sed 's/\(\s\+\)phone-\([0-9]\+\)/\1device-\2&/' file
于 2013-08-17T22:38:57.240 回答