Recently while working on formatting some data files for further processing, I had to remove duplicate lines from the file based on a particular field. After trying out
grep commands, I was finally able to solve it with a very concise
The command was so concise but still was packed with so much information and it helped me to learn more about the
awk scripting language. I thought of writing about it here so that it is useful for others and also I know where to search for it, when I needed it
Feel free to use it in whatever way you want, if it solves your problem as well.
Input and output data
Let me first explain the input data I had and the output that I was expecting.
Consider a file which has the following lines. Each line has four fields.
Now assume that we want to remove duplicate lines by comparing only the second field. We want the output to look like this.
Get ready for the surprise. The actual command is just this.
awk script execution and printing
awk script is executed for each line and if the result is
true then the line is printed. If the result is
false then the line is not printed.
awk language supports associate arrays, similar to the ones found in PHP. The script
x[$2]++ fills up an associate array. The key used here is
$2 which refers to the second field and
x is the variable name. You can use any name for it.
The array is populated for every line. This is how the array would look like after each line.
! operator results in a boolean evaluation which determines whether a particular line should be passed on to the output (printed) or not.
When the field is not present in the array, then it results in a zero value which is false. The
! (not) operator evaluates it to non-zero, which results in a true value and the line is passed on to the output (printed). When a duplicate is found, the array returns a non-zero count, which is true, but the
! converts it to false and that line is not passed on to the output.
The expanded version of the above command would be
But what is the fun in using the expanded version
In the input file that I had, the fields were separated by whitespace, so I didn’t have to specify the field separators. But if you are using a non-whitespace field separators, then you can specify it by adding
FS="," to the above command.
This one-liner actually thought me that
awk supports a full programming language that can be used to create scripts and also increased my understanding of the way
awk command works. Hopefully this teaches something for you as well
I know that this is already a concise version, but if you think that this can be improved, then do let me know.
- How to print unique errors(with count) from Apache error logs
- How To Unfreeze Frozen SSH Tabs In Mac Terminal App
- Rollback a commit in git, while maintaining history
- Sum up all values of a column in a text file
- Count the number of empty lines in a file using grep
- Passing command-line arguments to Pig scripts