Q: print a column based on the uniqe value of another

Hi there,

If I have the following:

and want to print the first column based on the uniqe value of the second column, so the output would be something like:

how would I do that, using AWK and the piping technique?

Thanks in advance

Hi

awk '!a[$2]++{print $1}' file

Guru

1 Like

thanks for the help guru,

unfortunely, it hasn't worked well.. as I got values in column $1 more than what $2 has

thanks for the help any way,, I really value that

---------- Post updated at 08:29 PM ---------- Previous update was at 06:42 PM ----------

I think I didn't explain that properly

what I meant is printing the first column while sorted by the second one on which "uniq" is applied

any help is highly valued

Can you provide us the contents inputfile (unless it is the same as before), the script you are using, and the contents of the outputfile?

1 Like

Thanks for your interest sir,

the input file is an access log file that keeps hold of IPs, files access, and the the transmission protocol, and so on..

If I want an output that contains the protocol and the file accessed using that protocol

given that protocol column is sorted using "uniq" command.

and that the file column is sorted by the protocol column.

so if I have something like:

it should look like this:

I hope that does made it clearer?

thank you again

But HTTP \img\333.jpg does not appear in the output?

1 Like

yep, only the first changing value in the protocol is printed in addition to the file value on the same line.

With awk:

awk 'p != $1 { print; } { p = $1; }' inputfile
1 Like

nope, that didn't work.. actually it pulled everything out :stuck_out_tongue:

Hm???

Here is my attempt:

mdludwig@tgrogdor$ cat datafile
FTP \file\1.rar
HTTP \img\4444443.jpg
HTTP \img\333.jpg
HTTP \img\35553.jpg
FTP \file\hello.rar
FTP \file\thanks_very_much.rar
HTTP \img\363.jpg

mdludwig@tgrogdor$ cat awkscript
p != $1 { print; } { p = $1; }

mdludwig@tgrogdor$ awk -f awkscript datafile
FTP \file\1.rar
HTTP \img\4444443.jpg
FTP \file\hello.rar
HTTP \img\363.jpg

Am I missing something?

1 Like

I think you are printing the whole line, while what I need is only printing the second column..

I've got 6 or so columns and there for I need to pick up the needed columns only.

Oh. Then just print the columns you want:

p != $1 { print $2; } { p = $1; }

In man awk (linux), the value of the first field is $1, the second is $2, and up to the number of fields (NF) whose value is $NF.

Hope this helps.

1 Like

m.d.ludwig,

I really can't find the word to thank you.. you are a lagend indeed...

many thanks