Comment On How Not to Count Lines

 "Not long ago, I was poking around one of our production web servers and noticed the following curiosity in a shell script," writes Marshall G. [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: How Not to Count Lines

2011-06-29 10:34 • by Frode Nescafen (unregistered)
First out

Re: How Not to Count Lines

2011-06-29 10:34 • by Dave-Sir (unregistered)
TMTOWTDI

Not spam, Askimet

Re: How Not to Count Lines

2011-06-29 10:34 • by Abdu (unregistered)
Frist!
PS: Not spam...

Re: How Not to Count Lines

2011-06-29 10:38 • by Anonymous Coward (unregistered)
But this version supports platforms where wc doesn't have -l.

Re: How Not to Count Lines

2011-06-29 10:39 • by C-Octothorpe
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...

Re: How Not to Count Lines

2011-06-29 10:43 • by Windozer (unregistered)
351549 in reply to 351547
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!

Re: How Not to Count Lines

2011-06-29 10:44 • by don
The final line of this comment was supposed to be my whole comment, but apparently it looks like spam to some algorithm:

Man!

Re: How Not to Count Lines

2011-06-29 10:45 • by justsomedude (unregistered)
351551 in reply to 351549
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


It's okay dear. Here, come sit down and play some minesweeper.

Re: How Not to Count Lines

2011-06-29 10:46 • by F (unregistered)
"... the author thought to use the wc command but not the -l option."

It's for flexibility. If you want to change it to report characters instead, you only need to change the parameters to cut, instead of having to replace wc -l with wc -c.

Re: How Not to Count Lines

2011-06-29 10:49 • by Steve The Cynic
It's obvious, can't you see? wc -l puts all those spaces on the front of the output, and you need all that gubbins to get rid of them!

It's not like this could do the job, is it?

wc -l < /var/run/netstat_r.lst | tr -d ' '

or even

wc -l < /var/run/netstat_r.lst | sed "s/ //g"

And no, I'm not a Linux geek. I'm a FreeBSD geek.

Re: How Not to Count Lines

2011-06-29 10:49 • by simple (unregistered)
351555 in reply to 351549
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...

Re: How Not to Count Lines

2011-06-29 10:49 • by kktkkr (unregistered)
Counting lines is easy:
Minus, Slash and Backslash - 1
Plus, Equals, Less Than, T - 2
Asterisk , H, K, F - 3
Octothorpe - Many.

Re: How Not to Count Lines

2011-06-29 10:50 • by anon y mouse (unregistered)
This smacks of someone doing something I WISH I had done to someone.

I was asked in a chatroom at work in all seriousness:

"Where is the /etc/hosts file??"

I wish my response had been as convoluted as that approach above....

Re: How Not to Count Lines

2011-06-29 10:53 • by dohpaz42
"Not long ago, I was poking around one of our production web servers and noticed the following curiosity in a shell script," writes Marshall G.

/bin/cat /var/run/netstat_r.lst | /usr/bin/wc | /usr/bin/cut -b -8 |perl -e 'my $r=<>; $r =~ s/^\s+//; print "$r"'

"Since the command only counts the number of lines in netstat_r.lst, I'm perplexed at the complexity of this approach."

"Especially since the author thought to use the wc command but not the -l option."


TRWTF is um perl?

Re: How Not to Count Lines

2011-06-29 10:54 • by C-Octothorpe
351562 in reply to 351555
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.

Re: How Not to Count Lines

2011-06-29 10:57 • by KattMan
351563 in reply to 351560
dohpaz42:

TRWTF is that Marshall G. didn't realize that this complex approach does not count lines that only contain whitespace; something that wc -l is not capable of doing.


So what you are saying is... there is no WTF, except for the OP not understanding the requirements of the script?

Re: How Not to Count Lines

2011-06-29 10:58 • by Hortical (unregistered)
351564 in reply to 351562
C-Octothorpe:
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.


Is yours just a one-man party?

Re: How Not to Count Lines

2011-06-29 10:58 • by dohpaz42
351565 in reply to 351563
Kattman:
So what you are saying is... there is no WTF, except for the OP not understanding the requirements of the script?

Actually TRWTF was my knee-jerk reaction trying to state a WTF, by forgetting that the input to perl is the output of wc. I've since updated my comment, but sadly not before you quoted me. :)

Re: How Not to Count Lines

2011-06-29 11:00 • by Warren (unregistered)
Couldn't you just determine the average number of characters per line and then divide the file size by this?

No I have realised this would only work if your characters were always one byte.

Re: How Not to Count Lines

2011-06-29 11:00 • by jarfil (unregistered)
351568 in reply to 351554
Steve The Cynic:
It's obvious, can't you see? wc -l puts all those spaces on the front of the output, and you need all that gubbins to get rid of them!


Don't know about BSD, but GNU's wc puts no spaces with -l. Also, it takes an input file as a parameter, so no need for pipes:

/usr/bin/wc -l /var/run/netstat_r.lst

Now, all this "could" be not such a WTF if /usr/bin/wc was some kind of redirect like from busybox or the likes, that wouldn't support the -l option... although whether it would be possible to run perl in such case, kind of baffles me.

Re: How Not to Count Lines

2011-06-29 11:00 • by C-Octothorpe
351569 in reply to 351564
Hortical:
C-Octothorpe:
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.


Is yours just a one-man party?


Of course! What, is there something else you're supposed to do while at work?

Re: How Not to Count Lines

2011-06-29 11:01 • by cihv (unregistered)
351570 in reply to 351549
I don't work with toilets on the command line so I count lines with awk:
awk '{lines++}END{print lines}' <filename>

Re: How Not to Count Lines

2011-06-29 11:03 • by C-Octothorpe (unregistered)
351571 in reply to 351564
Hortical:
C-Octothorpe:
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.


Is yours just a one-man party?


There's not much else to do where I live.

Re: How Not to Count Lines

2011-06-29 11:03 • by frits
351572 in reply to 351549
Windozer:
What's a perl?

I can find all the perls I need here.

Re: How Not to Count Lines

2011-06-29 11:06 • by DCRoss
I don't see why he couldn't have just read the manual page for wc to find out what options it supported. It's as easy as typing

echo | gunzip -c $(find /usr/share/man -name 'wc\.*' | head -1 |awk '/wc/ { print $1}' | tail -1 ) | sed '' | /usr/bin/groff -mtty-char -Tutf8 -mandoc | cat -u | pager -s


Seriously, what is wrong with people?




Re: How Not to Count Lines

2011-06-29 11:07 • by Pr0gramm3r (unregistered)
351574 in reply to 351554
Steve The Cynic:
It's obvious, can't you see? wc -l puts all those spaces on the front of the output, and you need all that gubbins to get rid of them!

It's not like this could do the job, is it?

wc -l < /var/run/netstat_r.lst | tr -d ' '

or even

wc -l < /var/run/netstat_r.lst | sed "s/ //g"

And no, I'm not a Linux geek. I'm a FreeBSD geek.

Same shit different pile.

Re: How Not to Count Lines

2011-06-29 11:08 • by Hortical (unregistered)
351575 in reply to 351569
C-Octothorpe:
Hortical:
C-Octothorpe:
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.


Is yours just a one-man party?


Of course! What, is there something else you're supposed to do while at work?


You have a job?!?! Someone pays you?!?!

Re: How Not to Count Lines

2011-06-29 11:09 • by ThePants999
351576 in reply to 351560
dohpaz42:
TRWTF is um perl?


Too right! The correct approach is obviously:

/bin/cat /var/run/netstat_r.lst | /usr/bin/wc | /usr/bin/cut -b -8 | awk '{print $1;}'

Re: How Not to Count Lines

2011-06-29 11:11 • by C-Octothorpe
351577 in reply to 351575
Hortical:
C-Octothorpe:
Hortical:
C-Octothorpe:
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.


Is yours just a one-man party?


Of course! What, is there something else you're supposed to do while at work?


You have a job?!?! Someone pays you?!?!


If by job you mean laid-off and by pay you mean UI, then yes, I have a paying job...

Re: How Not to Count Lines

2011-06-29 11:12 • by Hortical (unregistered)
351578 in reply to 351577
C-Octothorpe:
Hortical:
C-Octothorpe:
Hortical:
C-Octothorpe:
simple:
Windozer:
C-Octothorpe:
Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...


What's a wc? What's a perl? Why do all of these backslashes go the wrong way? What's going on?
HALP ME, I DON'T UNDERSTAND!!!


Never have I wanted to 'upvote' a reply more than I do now...


Right on queue: the circle-jerk begins.


Is yours just a one-man party?


Of course! What, is there something else you're supposed to do while at work?


You have a job?!?! Someone pays you?!?!


If by job you mean sitting at an internet cafe and by pay you mean not pay, then yes, I have a paying job...


So your life consists of jerking off in an internet cafe while talking to my many sockpuppets in comments threads on TDWTF.

I'll let that sink in.

Re: How Not to Count Lines

2011-06-29 11:13 • by PleegWat (unregistered)
351579 in reply to 351557
It is at C:\WINDOWS\SYSTEM\drv\etc\hosts

Re: How Not to Count Lines

2011-06-29 11:15 • by Jorrit (unregistered)
351580 in reply to 351554
Uhm, there are no formatting spaces when you do:

wc -l < FILENAME

But I suppose it's always a good idea to remove them. Just in case.

Re: How Not to Count Lines

2011-06-29 11:16 • by C-Octothorpe
351582 in reply to 351578
Hortical:
So your life consists of jerking off in an internet cafe while talking to my many sockpuppets in comments threads on TDWTF.

I'll let that sink in.


YHBT YHL HAND

I'll let that sink in.

Re: How Not to Count Lines

2011-06-29 11:17 • by Steve The Cynic
351583 in reply to 351579
PleegWat:
It is at C:\WINDOWS\SYSTEM\drv\etc\hosts

Not since Monkey Boy put the stake in the heart of Windows Me it's not.

%WINDOWS_DIRECTORY%\SYSTEM32\drivers\etc\hosts

YMMV on x64 systems.

Re: How Not to Count Lines

2011-06-29 11:19 • by Steve The Cynic
351584 in reply to 351580
Jorrit:
Uhm, there are no formatting spaces when you do:

wc -l < FILENAME

But I suppose it's always a good idea to remove them. Just in case.


On FreeBSD, wc -l displays spaces in front of the number, and if you give it filename args, it displays the name(s) after the numbers.

So just in case you aren't using GNU tools.

Re: How Not to Count Lines

2011-06-29 11:25 • by whiskeylover
CAN YOU SEND ME THOSE CODEZ TO MY AOL? I WANT TO HAK MY EX'S EMAL.

-SCRIPTKIDDIE29923

Re: How Not to Count Lines

2011-06-29 11:27 • by Chris (unregistered)
So... apparently this should return more than just the line count:

~$ wc /etc/hosts|cut -b -8
9 26

Re: How Not to Count Lines

2011-06-29 11:29 • by David (unregistered)
351589 in reply to 351563
Still could've done that much better:

/bin/sed '/^\W*$/d' /var/run/netstat_r.lst | wc -l


Could probably write an awk script that wouldn't need the pipe, but this is quite a bit more readable (if you're going to use regex's anyways).

Re: How Not to Count Lines

2011-06-29 11:29 • by Hortical (unregistered)
351590 in reply to 351582
C-Octothorpe:
Hortical:
So your life consists of jerking off in an internet cafe while talking to my many sockpuppets in comments threads on TDWTF.

I'll let that sink in.


YHBT YHL HAND

I'll let that sink in.


Hmmm...

You admit to being an expendable loser (laid-off) with a narrow scope of expertise (doesn't understand unix) and I'm supposed to feel like the joke's on me?

If it keeps you from killing yourself and entertaining us all day, I guess I'm fine with that.

Re: How Not to Count Lines

2011-06-29 11:30 • by random_garbage
351591 in reply to 351583
Steve The Cynic:
%WINDOWS_DIRECTORY%\SYSTEM32\drivers\etc\hosts

YMMV on x64 systems.
Actually, IIRC, for historical reasons, Win64 still keeps all the relevant 64-bit libraries, etc. in the System32 folder.

For maximum fun, the 32-bit compatibility libraries are stored in the WoW64 folder...

Figure that one out...

Re: How Not to Count Lines

2011-06-29 11:34 • by tk421 (unregistered)
351594 in reply to 351582
C-Octothorpe:
Hortical:
So your life consists of jerking off in an internet cafe while talking to my many sockpuppets in comments threads on TDWTF.

I'll let that sink in.


YHBT YHL HAND

I'll let that sink in.


Your terrible life is a troll? Or your pretending to have a terrible life is a troll? Or the fact that he's trolling you is a troll?

Re: How Not to Count Lines

2011-06-29 11:43 • by C-Octothorpe
351595 in reply to 351590
Hortical:
C-Octothorpe:
Hortical:
So your life consists of jerking off in an internet cafe while talking to my many sockpuppets in comments threads on TDWTF.

I'll let that sink in.


YHBT YHL HAND

I'll let that sink in.


Hmmm...

You admit to being an expendable loser (laid-off) with a narrow scope of expertise (doesn't understand unix) and I'm supposed to feel like the joke's on me?

If it keeps you from killing yourself and entertaining us all day, I guess I'm fine with that.


Well, the joke *is* on you because if you're too up-tight to recognize an obvious joke, then I feel really bad for you...

Don't let me get in the way: nerd on brother!

Re: How Not to Count Lines

2011-06-29 11:44 • by whiskeylover
351596 in reply to 351595
C-Octothorpe:
nerd on brother!

Hehe

Re: How Not to Count Lines

2011-06-29 11:46 • by EvilSpudBoy (unregistered)
351597 in reply to 351557
You could have told him, "I'll send it to you." And then send, via interoffice, a manila folder labeled /etc/hosts containing a typewritten list of host names.

Re: How Not to Count Lines

2011-06-29 11:46 • by trtrwtf (unregistered)
351598 in reply to 351595
C-Octothorpe:
Don't let me get in the way: nerd on brother!


Mr. Kettleblack, there is a call for you, a Mr. Pot is on the line.

Please post less, you bore

2011-06-29 11:47 • by Nagesh (unregistered)
C-Octomom's "jokes" are a crime against comedy.

Re: How Not to Count Lines

2011-06-29 11:50 • by trtrwtf (unregistered)
351600 in reply to 351598
trtrwtf:
C-Octothorpe:
Don't let me get in the way: nerd on brother!


Mr. Kettleblack, there is a call for you, a Mr. Pot is on the line.

Not everybody can be a dashing, sexy wordsmith like yours truly.

Re: How Not to Count Lines

2011-06-29 11:57 • by SG_01 (unregistered)
351602 in reply to 351557
anon y mouse:
"Where is the /etc/hosts file??"


In C:\Windows\System32\Drivers of course ;)

Re: How Not to Count Lines

2011-06-29 11:58 • by Too Lazy To Sign In (unregistered)
351604 in reply to 351591
Well, WoW64 does make a twisted kind of sense if you write it out - Windows on Windows64.

Re: How Not to Count Lines

2011-06-29 12:01 • by davee123 (unregistered)
This isn't a WTF, it's job security!

DaveE
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment