How to sort/omit file output?

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

How to sort/omit file output?

Postposted on Sun Mar 08, 2009 12:45 pm

Hey Unix/Linux guys/gals, how would I go about taking the output of the "who" command, writing it to a file while reordering the output and ommiting some things? Normally entering "who" would give the following output:

jackie pts/25 Mar 02 09:16 (adsl-71-134-158-92.dsl.pltn38.pacbell.net)

But I would like to take that output, write it to a file, and have it look like this:

Mar 02 09:16 pts/25 jackie

I know the ">" flag can be used to send output to a file, and maybe some variation of the "sort" "cut" "paste" commands can be used.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: How to sort/omit file output?

Postposted on Sun Mar 08, 2009 12:54 pm

Ok, im getting a little further. Entering:

who > whooutput
cut -f1 whooutput

produces the following result:

crp pts/3 Mar 8 10:33
dmitriym pts/4 Mar 8 10:36

(formatting is all messed up, columns actually appear uniform in console)
Now how do I sort it?
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: How to sort/omit file output?

Postposted on Sun Mar 08, 2009 1:10 pm

How do you want it sorted? What information do you want to have on each line?
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: How to sort/omit file output?

Postposted on Sun Mar 08, 2009 1:16 pm

Which field are you trying to sort by? Or do you just mean the re-ordering of the columns?

I would use some variation of:
Code: Select all
who | awk '{ printf("%s %s %s %s\n", $3, $4, $2, $1) }'

You can add field width specifiers to the format string to get fixed-width columns if you want. To sort by user name (or some other field), just pipe the output to sort, giving the appropriate --key option.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37514
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: How to sort/omit file output?

Postposted on Sun Mar 08, 2009 2:06 pm

Here's how I got it to work but there has to be a much more efficient method:

libra% who > whooutput
libra% cut -c1-8 whooutput > names
libra% cut -c12-16 whooutput > terminal
libra% cut -c25-36 whooutput > timedate
libra% paste timedate terminal names > finaloutput
libra% cat finaloutput
Mar 8 10:54 pts/3 cshall
Mar 8 10:52 pts/2 dmitriym
Mar 8 10:58 pts/4 consoli
Mar 8 10:58 pts/5 crp
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: How to sort/omit file output?

Postposted on Sun Mar 08, 2009 2:24 pm

dmitriylm wrote:Here's how I got it to work but there has to be a much more efficient method:

See the post right above yours. :wink:
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37514
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: How to sort/omit file output?

Postposted on Mon Mar 09, 2009 9:36 am

This thread came at just the right time for me!

Just used
Code: Select all
awk 'Blocked {print $11} /var/log/mail.log | sort | uniq > spammers.txt

to get a list of all the email addresses that have sent spam to a server.

was that an OK way to do this? Presumably awk could have done the whole job without all the pipes, not that doing it this way took any time to run.
Fernando!
Your mother ate my dog!
cheesyking
Minister of Gerbil Affairs
 
Posts: 2245
Joined: Sun Jan 25, 2004 7:52 am
Location: That London (or so I'm told)

Re: How to sort/omit file output?

Postposted on Mon Mar 09, 2009 11:10 am

cheesyking wrote:Just used
Code: Select all
awk 'Blocked {print $11} /var/log/mail.log | sort | uniq > spammers.txt

to get a list of all the email addresses that have sent spam to a server.

was that an OK way to do this? Presumably awk could have done the whole job without all the pipes, not that doing it this way took any time to run.

One thing you can do to eliminate one pipe here (and wherever you use the same pattern) is to replace "sort | uniq" with "sort -u"
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 4:39 pm
Location: Mountain View, CA


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 3 guests