how to delete the duplicate lines in file except the first matched line

前端 未结 2 1663
北荒
北荒 2020-12-02 02:36

In the following configuration file

/etc/fine-tune.conf

We have duplicate lines as

clean_history_in_os=true
2条回答
  •  醉话见心
    2020-12-02 03:15

    You can use this awk to delete all matching lines except the first one:

    awk '!(/clean_history_in_os=true/ && n++)' file
    

    To save file in place you can use this gnu awk command:

    awk -i inplace '!(/clean_history_in_os=true/ && n++)' file
    

    otherwise use temporary file as:

    awk '!(/clean_history_in_os=true/ && n++)' file > $$.tmp && mv $$.tmp file
    

    Here is one sed solution to do the same:

    sed -i -n '0,/clean_history_in_os=true/p;/clean_history_in_os=true/!p' file
    

提交回复
热议问题