уникальный файл на основе двухстрочного совпадения

У меня есть файл с такими строками. Я хотел бы сделать это уникальным, где каждый уникальный элемент состоит из двух строк. так с тех пор

bob
100

здесь дважды, я бы напечатал его только один раз. Помогите, пожалуйста. Благодарность,

bob
100
bill
130
joe
123
bob
100
joe
120

person user3684544    schedule 01.05.2015    source источник


Ответы (3)


Попробуй это:

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

Выход:

bill
130
bob
100
joe
120
joe
123

Со встроенными bash:

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
person Cyrus    schedule 01.05.2015

Попробуйте sed:

sed 'N;s/\n/ /' file | sort -u | tr ' ' '\n'
  • N: прочитать следующую строку и добавить к текущей строке
  • ;: разделитель команд
  • s/\n/ /: заменить eol на пробел
person kev    schedule 01.05.2015

Я бы использовал 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}
person hek2mgl    schedule 01.05.2015