基于2行匹配的唯一文件

时间:2015-05-01 17:14:08

标签: bash sorting uniq

我有一个包含这样的行的文件。我想在这里将每个独特的项目都包含在2行中。所以

bob
100

在这里两次我只打印一次。请帮忙。感谢,

bob
100
bill
130
joe
123
bob
100
joe
120

3 个答案:

答案 0 :(得分:2)

试试这个:

printf "%s %s\n" $(< file) | sort -u | tr " " "\n"

输出:

bill
130
bob
100
joe
120
joe
123

使用bash builtins:

declare -A a            # declare associative array
while read name; do read value; a[$name $value]=; done < file
printf "%s\n" ${!a[@]}  # print array keys

输出:

joe
120
joe
123
bob
100
bill
130

答案 1 :(得分:1)

我会使用awk

awk 'NR%2{l=$0}!(NR%2){seen[l"\n"$0]}END{for(i in seen)print i}' input

让我以多行版本解释命令:

# On odd lines numbers store the current line in l.
# Note that line numbers starting at 1 in awk
NR%2     {l=$0}

# On even line numbers create an index in a associative array.
# The index is the last line plus the current line.
# Duplicates would simply overwrite themselves.
!(NR%2)  {seen[l"\n"$0]}

# After the last line of input has been processed iterate
# through the array and print the indexes
END      {for(i in seen)print i}

答案 2 :(得分:1)

尝试sed

sed 'N;s/\n/ /' file | sort -u | tr ' ' '\n'
  • N:阅读下一行,并附加到当前行
  • ;:命令分隔符
  • s/\n/ /:用空格替换eol