我有一个每天下载的电影列表,格式如下
act-of-valor-2012
act-of-valor-2012
the-possession-2012
the-possession-2012
american-crude-2008
american-crude-2008
when-strangers-appear-2001
like-father-like-son-2005
like-father-like-son-2005
get-him-to-the-greek-2010
get-him-to-the-greek-2010
wall-street-money-never-sleeps-2010
wall-street-money-never-sleeps-2010
christmas-evil-1980
days-of-glory-1944
我需要保持相同的顺序,但删除重复的行,这样就变成了
act-of-valor-2012
the-possession-2012
american-crude-2008
when-strangers-appear-2001
like-father-like-son-2005
get-him-to-the-greek-2010
wall-street-money-never-sleeps-2010
christmas-evil-1980
days-of-glory-1944
排序会改变顺序,有人知道我该如何实现这一点吗?
谢谢
答案1
您想要uniq
命令。
从man uniq
:
Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).
With no options, matching lines are merged to the first occurrence.
使用方法如下:
uniq movies.txt
按要求输出。
答案2
AWK 版本:
awk '{array[i++]=$0}END{for(j=0;j<=i;j++) if(array[j] != array[j-1]){print array[j]} }' movies.txt
基本上,它的作用是将数据读入数组并仅打印出没有重复前一行的行。
更短的方法是像这样这里:
$ awk '!a[$0]++' movies.txt
act-of-valor-2012
the-possession-2012
american-crude-2008
when-strangers-appear-2001
like-father-like-son-2005
get-him-to-the-greek-2010
wall-street-money-never-sleeps-2010
christmas-evil-1980
days-of-glory-1944