What are some single-line lines that can output unique elements of the nth column to another file?

I have a file like this:

1 2 3 
4 5 6
7 6 8
9 6 3
4 4 4

What are some single-line lines that can output unique elements of the nth column to another file?

EDIT: Here is a list of the solutions that people have given. Thanks guys!

cat in.txt | cut -d' ' -f 3 | sort -u
cut -c 1 t.txt | sort -u
awk '{ print $2 }' cols.txt | uniq
perl -anE 'say $F[0] unless $h{$F[0]}++' filename
+3
source share
5 answers

Fixed: Thank you, Mark Rushakov.

$ cut -c 1 t.txt | sort | uniq

or

$ cut -c 1 t.txt | sort -u


1
4
7
9
+6
source

In perl to 5.10

perl -lane 'print $F[0] unless $h{$F[0]}++' filename

In Perl after 5.10

perl -anE 'say $F[0] unless $h{$F[0]}++' filename

Replace 0with the column you want to output.

For j_random_hacker, here is an implementation that will use very little memory (but will be slower and require more input):

perl -lane 'BEGIN {dbmopen %h, "/tmp/$$", 0600; unlink "/tmp/$$.db" } print $F[0] unless $h{$F[0]}++' filename

dbmopen DBM ( ) % h. , % h, , . unlink , , ( POSIX ).

+10

:

$ cat in.txt | cut -d' ' -f 3 | sort -u
3
4
6
8

cut -d' ' , , -f 3 . , sort -u , .

+3
source

Say your file is "cols.txt" and you need the unique elements of the second column:

awk '{ print $2 }' cols.txt | uniq

You can find the following article useful for more information about these utilities:

+3
source

if you use awk, you do not need to use other commands

awk '!_[$2]++{print $2}' file
+2
source

Source: https://habr.com/ru/post/1715141/


All Articles