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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Q: print a column based on the uniqe value of another
# 1  
Old 01-04-2011
Q: print a column based on the uniqe value of another

Hi there,

If I have the following:

Quote:
a 2
b 4
c 5
d 4
e 5
and want to print the first column based on the uniqe value of the second column, so the output would be something like:

Quote:
a
b
c
how would I do that, using AWK and the piping technique?

Thanks in advance
# 2  
Old 01-04-2011
Hi

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

Guru
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 01-04-2011
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
# 4  
Old 01-04-2011
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?
This User Gave Thanks to m.d.ludwig For This Post:
# 5  
Old 01-04-2011
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:
Quote:
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
it should look like this:


Quote:
FTP \file\1.rar
HTTP \img\4444443.jpg
FTP \file\hello.rar
HTTP \img\363.jpg
I hope that does made it clearer?

thank you again
# 6  
Old 01-04-2011
But HTTP \img\333.jpg does not appear in the output?
This User Gave Thanks to m.d.ludwig For This Post:
# 7  
Old 01-04-2011
yep, only the first changing value in the protocol is printed in addition to the file value on the same line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print column based on pattern

Hi all, how print on columns when contain un pattern specific, e.g. $cat file1 3234 234 2323 number1 number2 number3 123 242 124 124 number2 324 424 543 626 number1 3463 234 534 345 number3 6756 345 2352 334 345 234 need output file1 way (2 Replies)
Discussion started by: aav1307
2 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Print the column content based on the header

i have a input of csv file as below but the sequence of column get changed. I,e it is not necessary that name comes first then age and rest all, it may vary. name,age,marks,roll,section kevin,25,80,456,A Satch,23,56,789,B Meena,24,78,H245,C So i want to print that column entires which... (12 Replies)
Discussion started by: millan
12 Replies

4. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

5. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

6. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

7. Shell Programming and Scripting

How to print column based on row number

Hi, I want to print column value based on row number say multiple of 8. Input file: line 1 67 34 line 2 45 57 . . . . . . line 8 12 46 . . . . . . line 16 24 90 . . . . . . line 24 49 67 Output 46 90 67 (2 Replies)
Discussion started by: Surabhi_so_mh
2 Replies

8. Shell Programming and Scripting

Need to print last column based on the value of first 2 colums

Hi Everybody. I need an urgent help, it would be great if somebody will help me out in this regard. See below sample file 1525 805 26 2036 219644. 2598293. 1525 805 126 2327 1525 805 226 ... (6 Replies)
Discussion started by: syahmed
6 Replies

9. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies

10. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies
Login or Register to Ask a Question