模式 - 保留第一列的第一个实例并替换其他实例

时间:2013-04-12 06:25:41

标签: perl bash unix

输入:

    Process1 |5f|03/29/13 15:32:59
    Process1 |6f|03/29/13 17:59:03
    Pro cess1 |s7|03/29/13 17:59:05
    Pro cess1 |1234|03/29/13 20:33:43
    Pro cess1 |L|03/29/13 20:33:54
    A Pro cess |45g|03/29/13 20:34:59
    A Pro cess |f4|03/29/13 21:10:22
    B Pro cess |Es|03/30/13 00:11:25
    B Pro cess |23|03/30/13 00:47:20
    B Pro cess |E23|03/30/13 01:03:07

预期输出:

Process1 |5f|03/29/13 15:32:59
 |6f|03/29/13 17:59:03
Pro cess1|s7|03/29/13 17:59:05
 |1234|03/29/13 20:33:43
 |L|03/29/13 20:33:54
A Pro cess |45g|03/29/13 20:34:59
 |f4|03/29/13 21:10:22
B Pro cess |Es|03/30/13 00:11:25
 |23|03/30/13 00:47:20
 |E23|03/30/13 01:03:07

逻辑:只保留第一列的一个实例,并将其他实例替换为空白

我尝试了这个,但没有用(在循环中使用下面的代码):

sed -e 's/Process1/ /g' -e 's/ /Process1/1' Input

(将Process1替换为' - ',然后再次将第一个' - '替换为Process1。

由“cnicutar”给出,我将awk修改为:

awk -F“|” “{A [$ 1] ++; if(a [$ 1]> 1)$ 1 =“”;打印;}'文件

来自abv的输出:

Process1 |5f|03/29/13 15:32:59
 6f 03/29/13 17:59:03
Pro cess1 |s7|03/29/13 17:59:05
 1234 03/29/13 20:33:43
 L 03/29/13 20:33:54
A Process |45g|03/29/13 20:34:59
 f4 03/29/13 21:10:22
B Pro cess |Es|03/30/13 00:11:25
 23 03/30/13 00:47:20
 E23 03/30/13 01:03:07

所需的输出(插入|)

Process1 |5f|03/29/13 15:32:59
     |6f|03/29/13 17:59:03
Pro cess1 |s7|03/29/13 17:59:05
     |1234|03/29/13 20:33:43
     |L|03/29/13 20:33:54
A Process |45g|03/29/13 20:34:59
     |f4|03/29/13 21:10:22
B Pro cess |Es|03/30/13 00:11:25
     |23 | 03/30/13 00:47:20
     |E23| 03/30/13 01:03:07

1 个答案:

答案 0 :(得分:2)

略微横向解决方案如何:

[cnicutar@ariel ~]$ awk '{a[$1]++; if (a[$1] > 1) $1=""; print;}' file
Process1 |5f|03/29/13 15:32:59
 |6f|03/29/13 17:59:03
 |s7|03/29/13 17:59:05
 |1234|03/29/13 20:33:43
 |L|03/29/13 20:33:54
AProcess |45g|03/29/13 20:34:59
 |f4|03/29/13 21:10:22
BProcess |Es|03/30/13 00:11:25
 |23|03/30/13 00:47:20
 |E23|03/30/13 01:03:07

根据编辑,您可能需要:

awk -F'|' '{OFS="|"; a[$1]++; if (a[$1] > 1) $1=" "; print;}' file