views:

151

answers:

2

Contents of part3.1.awk

{
current_line=$0
if (current_line!=prev)
{
    print $1 " -> " " -> " $5 " -> " $8
}
prev=$0
}

To get the list of processes, i run this in terminal. I want to get output with removed duplicates and sorted too.

$ps -ef | awk -f part3.1.awk | sort

What wrong am i doing?

+2  A: 

You are sorting the output from the awk script, when you want to be sorting the input.

$ps -ef | awk -f part3.1.awk | sort

should be

$ps -ef | sort | awk -f part 3.1.awk

But I should tell you that you don't need awk to remove duplicates. sort -u will do this for you, as in

ps -ef | sort -u

danben
sorry.. i wrote wrong.. i will edit my question.. My main aim is to remove the duplicates...
shadyabhi
Yes, this should work. The problem is that your awk script relies on all duplicates occurring consecutively. This is why you need to sort BEFORE passing the values to awk, rather than after.
danben
And it is still the case that `sort -u` will give you sorted values without duplicates.
danben
Hopy crap.. I searched the manual of "sort" for "duplicate" but didnt find anything, so posted the question.. I got what i wanted.. Thanx
shadyabhi
+1  A: 

try using

$ ps -ef | sort | uniq
Paul Creasey
How can i do it without using uniq...???
shadyabhi
Ok. i got it.. i can use "sort -u"
shadyabhi
sort -u is fine, i tend to use uniq since the intention is clear even when the reader hasn't seen the command before.
Paul Creasey
yup.. i agree with you.
shadyabhi