在重复的名称上添加双引号

时间:2017-04-06 19:31:04

标签: awk sed grep

我想从下面的文件中找到重复的名称,并用""标记它们。

文件:

James Miki:123-456-7890
Wang Tai:  234-563-6879
James Miki: 123-456-7890

输出想要看起来像:

"James Miki": 123-456-7890

谢谢。

4 个答案:

答案 0 :(得分:1)

$ awk -F: 'a[$1]++ {print "\"" $1 "\"" FS $2}' file "James Miki": 123-456-7890 救援!

foreach(ExcelSDRAddIn.UserControlSDR.Classification variableName in classArray[i])

答案 1 :(得分:0)

试试这个 -

cat f
James Miki:123-456-7890
Wang Tai: 234-563-6879
James Miki: 123-456-7890
James Miki: 456-456-8888     ### added for test case
Wang Tai: 234-563-6879       ### added for test case
Vipin Kumar : 878-432-2345   ### added for test case
Vipin Kumar : 878-432-2345   ### added for test case

awk -F':' '{gsub(/ /,"",$2)}{b[$1FS$2]++} END {for(k in b) if(b[k]>1) {split(k,u,":"); print v,u[1],v FS u[2]}}' v='"' OFS=""  f
"Vipin Kumar ":878-432-2345
"Wang Tai":234-563-6879
"James Miki":123-456-7890

解释 -

gsub(/ /,"",$2): Remove space from 2nd column
b[$1FS$2]++    : Create array b and store col1 and 2 
if(b[k]>1)     : Check duplicate record
split(k,u,":") : split the stored value in k (combination of col1 and 2) so that we can add double quote on first column.

答案 2 :(得分:0)

sed 's/: */:/' FILE | awk -F: '{ if (arr[$1":"$2]) print "\""$1"\":"$2; else { arr[$1":"$2]++; print $0 }}'

答案 3 :(得分:0)

使用sed + sort + uniq管道的另一种选择:

cat file | sed 's/^\(.*\) *: */"\1": /' | sort | uniq -d

输出:

"James Miki": 123-456-7890