# How to remove duplicate lines from files preserving their order

DevFeed: [How to remove duplicate lines from files preserving their order](<https://devfeed.tech/articles/how-to-remove-duplicate-lines-from-files-preserving-their-order-37447.md>)

Original publisher: [Read original article](<https://iridakos.com/programming/2019/05/16/remove-duplicate-lines-preserving-order-linux>)

Author: Lazarus Lazaridis

Published: 2019-05-16T08:30:00Z

Content type: tutorial

Language: en

Sources: [Lazarus Lazaridis](<https://devfeed.tech/sources/lazarus-lazaridis.md>)

Topics: [Script](<https://devfeed.tech/topics/script.md>), [file](<https://devfeed.tech/topics/file.md>)

Tags: [approach](<https://devfeed.tech/tags/approach.md>), [awk](<https://devfeed.tech/tags/awk.md>), [bash](<https://devfeed.tech/tags/bash.md>), [featured](<https://devfeed.tech/tags/featured.md>), [file](<https://devfeed.tech/tags/file.md>), [files](<https://devfeed.tech/tags/files.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [linux](<https://devfeed.tech/tags/linux.md>), [programming](<https://devfeed.tech/tags/programming.md>), [scripts](<https://devfeed.tech/tags/scripts.md>)

## AI overview

A tutorial explains how to remove duplicate lines from a text file with awk while preserving their original order. It breaks down the associative array, line-occurrence tracking, negation, post-increment behavior, and why other approaches may not preserve order.

## Source excerpt

Suppose you have a text file and you need to remove all of its duplicate lines. TL;DR To remove the duplicate lines preserving their order in the file use: awk '!visited[$0]++' your_file > deduplicated_file How it works The script keeps an associative array with indices equal to the unique lines of the file and values equal to their occurrences. For each line of the file, if the line occurrences are zero then it increases them by one and prints the line, otherwise it just increases the occurrences without printing the line. I was not familiar with awk and I wanted to understand how is this accomplished with such a short script (awkward). I did my research and here is what is going on: the awk "script" !visited[$0]++ is executed for each line of the input file visited[] is a variable of type associative array (a.k.a. Map). We don't have to initialize it, awk will do this for us the first time we access it. the $0 variable holds the contents of the line currently being processed visited[$0] accesses the value stored in the map with key equal to $0 (the line being processed), a.k.a. the occurrences (which we set below) the ! negates the occurrences value: In awk, any nonzero numeric value or any nonempty string value is true By default, variables are initialized to the empty string, which is zero if converted to a number That being said: if visited[$0] returns a number greater than zero, this negation is resolved to false. if visited[$0] returns a number equal to zero or an empty string, this negation is resolved to true. the ++ operation increases the variable's value (visited[$0]) by one. If the value is empty, awk converts it to 0 (number) automatically and then it gets increased. Note: the operation is executed after we access the variable's value. Summing up, the whole expression evaluates to: true if the occurrences are zero/empty string false if the occurrences are greater than zero awk statements consist of a pattern-expression and an associated action. <patter