如何获取一个基于第 11 至第 21 列的 shell 脚本来删除文本文件中的重复项?
样本文件:
Header:0000000000000001457854500000
XP 12345678912yeyeyeyeeye 0000003
XP 12345678913yeyeyeyeeye 0000002
XP 12345678912yeyeyeyeeye 0000004
XP 12345678913yeyeyeyeeye 0000001
Footer:0000000000000001245856500004
预期输出:
Header:0000000000000001457854500000
XP 12345678913yeyeyeyeeye 0000001
Xp 12345678912yeyeyeyeeye 0000004
Footer:0000000000000001245856500001
答案1
根据您的预期输出,可能类似于:
awk 'NF <= 1 || !seen[substr($0, 11, 11)]++'
或者
awk 'NF <= 1 || !seen[substr($2, 1, 11)]++'
或者保留最后的记录:
awk '!second_pass {if (NF > 1) count[substr($2, 1, 11)]++; next}
NF <= 1 || --count[substr($2, 1, 11)] == 0' file second_pass=1 file
答案2
命令:header=
sed -n '1p' l.txt ; footer=
sed -n '$p' l.txt;sed -e '1d' -e '$d' l.txt |awk '{if (!seen[$2]++)print $0}'| sed '1i '$header''| sed '$s/.*/&\n'$footer'/g'
输出
header=`sed -n '1p' l.txt`; footer=`sed -n '$p' l.txt`;sed -e '1d' -e '$d' l.txt |awk '{if (!seen[$2]++)print $0}'| sed '1i '$header''| sed '$s/.*/&\n'$footer'/g'
Header:0000000000000001457854500000
XP 12345678912yeyeyeyeeye 0000003
XP 12345678913yeyeyeyeeye 0000002
Footer:0000000000000001245856500004