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
# 8  
Old 01-04-2011
With awk:
Code:
awk 'p != $1 { print; } { p = $1; }' inputfile

This User Gave Thanks to m.d.ludwig For This Post:
# 9  
Old 01-04-2011
nope, that didn't work.. actually it pulled everything out :P
# 10  
Old 01-04-2011
Hm???

Here is my attempt:
Code:
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?
This User Gave Thanks to m.d.ludwig For This Post:
# 11  
Old 01-04-2011
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.
# 12  
Old 01-04-2011
Oh. Then just print the columns you want:
Code:
p != $1 { print $2; } { p = $1; }

In awk, 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.
This User Gave Thanks to m.d.ludwig For This Post:
# 13  
Old 01-04-2011
m.d.ludwig,

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

many thanks
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