News for hackers. Stuff that matters.
Re:Basic Command-Line Data Processing in Linux by pbrumm on Sunday August 29, 2010 @10:00PM (#1645156)
Even after using linux command line for years it is always useful to see how others use it. I tend to overuse perl regex's to massage the data how I want it and underuse awk.

my norm is cat file | perl -p -e "s/.from ([0-9\.]+) ./\1/g" to get the ip out of the same datafile.

regex's seem to help with messy data or data that contains inconsistent delimiters.

(some of the stars got stripped by HN so the above won't work)

Re:Basic Command-Line Data Processing in Linux by nitrogen on Monday August 30, 2010 @03:43AM (#1645644)
(some of the stars got stripped by HN so the above won't work)

Try putting a couple of spaces in front of your code line, like this:

  cat file | perl -p -e "s/.*from ([0-9\.]+).*/\1/g"
See http://news.ycombinator.com/formatdoc

Edit: for simple regexes, sed works well, too, and probably loads slightly faster than perl.

Re:Basic Command-Line Data Processing in Linux by jimbokun on Sunday August 29, 2010 @11:01PM (#1645254)
Also, tools like Awk and friends can be ridiculously fast and concise.

http://anyall.org/blog/2009/09/dont-mawk-awk-the-fastest-and...

Re:Basic Command-Line Data Processing in Linux by j_baker on Monday August 30, 2010 @01:45AM (#1645502)
I just learned about another one a couple of days ago: cut. Can't believe I never knew of its existence.

Also, for programmers, I'd recommend ack over grep.

Re:Basic Command-Line Data Processing in Linux by tsmall on Monday August 30, 2010 @08:25AM (#1646006)
Thanks for the tip. I'd never heard of that one either. It seems like a simpler awk, or at least small subset of awk.
Re:Basic Command-Line Data Processing in Linux by schm00 on Monday August 30, 2010 @12:22AM (#1645384)
I started a wikibook on this stuff a few years back. Includes material on inline perl, gnuplot and has lots of examples. Check out: http://en.wikibooks.org/wiki/Ad_Hoc_Data_Analysis_From_The_U...
Re:Basic Command-Line Data Processing in Linux by Mod_daniel on Monday August 30, 2010 @08:45PM (#1648039)
So very cool! Thank you for posting.
Re:Basic Command-Line Data Processing in Linux by wonderzombie on Monday August 30, 2010 @04:07PM (#1647384)
For my part, I don't use awk for anything more complicated than one-liners. I used it for a while, stopped when I was working on something else, and forgot all the awk-specific stuff.

My MO these days is if it's anything more complicated than an awk one-liner like awk '{print $2 " " $NF}', I'll use Python or, lately, Ruby. (Perl would be fine, too, if I used it in other contexts often enough.)

That said, there's nothing quite like, well, programming your environment. The extent to which you can manipulate files, directories, and text in *nix right out of the box makes me feel privileged to understand it. I can remember a time when renaming a bunch of images en masse seemed tantalizing but out of reach. I've since learned quite a bit, and even though it's relatively mundane now, it still feels magical. Upthread, someone called it "moving mountains." That's precisely it, and I love it.

Yes, yes. I'm a complete and utter nerd, etc.

Re:Basic Command-Line Data Processing in Linux by muyyatin on Sunday August 29, 2010 @08:17PM (#1644963)
It's also useful to know that 'sort -u' removes duplicates.
Re:Basic Command-Line Data Processing in Linux by jimbokun on Sunday August 29, 2010 @10:45PM (#1645228)
Although it was important to use the -c flag on uniq for this particular problem, which is not available with "sort -u."

Which I guess just goes to reinforce the Unix philosophy of tools that do one job and do it well.

Re:Basic Command-Line Data Processing in Linux by mturmon on Monday August 30, 2010 @01:33AM (#1645488)
They forgot sed.
Re:Basic Command-Line Data Processing in Linux by bobf on Tuesday August 31, 2010 @02:27AM (#1648638)
As others have mentioned, tr and cut are extremely useful. Although I had overlooked them in the past, expand/unexpand are also very useful! They convert tabs to spaces, or spaces to tabs. Of course there are other ways to do that, like substituting with sed, translating with tr, or printing tabbed data using $1/$2/etc. with awk... they just aren't as simple.
Re:Basic Command-Line Data Processing in Linux by KC8ZKF on Sunday August 29, 2010 @09:10PM (#1645077)
See also "Opening the software toolbox" by Arnold Robbins, part of the GNU Coreutils documentation.

http://www.gnu.org/software/coreutils/manual/html_node/Openi...

Re:Basic Command-Line Data Processing in Linux by ludwigvan on Monday August 30, 2010 @08:34AM (#1646026)
GNU Coreutils documentation as a whole is very useful.

http://www.gnu.org/software/coreutils/manual/ To read on the command line, try:

info coreutils

Also see,

http://en.wikipedia.org/wiki/GNU_Core_Utilities

Re:Basic Command-Line Data Processing in Linux by ddelony on Sunday August 29, 2010 @08:28PM (#1644988)
Hacker News readers probably have at least a passing familiarity with Unix/Linux, but it's still refreshing to be reminded that you can move mountains with short commands.
Re:Basic Command-Line Data Processing in Linux by kd0amg on Sunday August 29, 2010 @10:07PM (#1645171)
And even for someone who knows all of this, knowing a good guide makes it easy to handle requests for help (often preemptively). This is one I can (and just did) send to a friend who's less familiar with Unix.
Re:Basic Command-Line Data Processing in Linux by Andrew-Dufresne on Monday August 30, 2010 @05:05AM (#1645726)
I think paste (merge lines of files) also deserves mention. Besides that, I have found tail and column to be extremely useful.
Re:Basic Command-Line Data Processing in Linux by obsessive1 on Sunday August 29, 2010 @08:33PM (#1645000)
I've been using Linux for a while now, and I never thought about how powerful those simple commands can be.
Re:Basic Command-Line Data Processing in Linux by riffraff on Monday August 30, 2010 @10:45AM (#1646312)
sinc people already pointed out the missing ack & comm, I'll add: no love for tr?
Re:Basic Command-Line Data Processing in Linux by caf on Monday August 30, 2010 @04:33AM (#1645691)
Best textutil you probably haven't heard of (or have forgotten about): comm(1)
Re:Basic Command-Line Data Processing in Linux by RiderOfGiraffes on Monday August 30, 2010 @04:40AM (#1645701)
I find "diff -y" more intuitive, but I didn't know comm and will explore potential uses.