I am more comfortable with awk, so I would like to present two solutions in awk.
Solution 1
$ echo abc,10.11.13.14,def,1.2.3.4,geh,6.7.54.23 | awk -F, '{for (i=1; i<NF; i+=2) if ($i == "def") print $(i+1)}'
1.2.3.4
In this case, I am looking for a machine name "def", if found, print the next column.
Solution 2
$ echo abc,10.11.13.14,def,1.2.3.4,geh,6.7.54.23 | tr , \\n | awk '/def/ {getline;print}'
1.2.3.4
In this solution, I use the tr
command to convert commas to new line, search for "def" and print the line that follows. I hope these solutions work for you.
awk '{ if ($1 != $2 ) print $1" "$2; }' file
Just replace file for the appropriate file.
Or as @manatwork mentioned in the comments and simpler
awk '$1!=$2' file
Best Answer
That's what the
uniq
standard command is for.Note that some
uniq
implementations like GNUuniq
will give you the first of a sequence of lines that sort the same (wherestrcoll()
returns 0) as opposed to are byte-to-byte identical (wherememcmp()
orstrcmp()
returns 0). To force a byte to byte comparison regardless of theuniq
implementation, you can force the locale toC
with: