• (cs) in reply to realmerlyn
    realmerlyn:
    Camel:
    When perl is allowed, here's my one-liner:
    perl -e 'print @{[<>]}."\n"' /var/run/netstat_r.lst
    

    I don't believe that does what you think it does. It'll do the same thing as:

    perl -e '@list = <>; print "@list\n";'
    

    which will merely print the entire file with spaces prepended to all lines but the first.

    No he has the array in scalar context.

  • rm5248 (unregistered)

    Oh man, the irony... Just yesterday my coworker and I wrote a bash script to count the number of lines in our project. Apparently we could have done it much easier. CURSE YOU, MARK BOWYTZ FOR NOT DOING THIS YESTERDAY!! fist shake

    (oh, and just for reference: what we did was find all the files in the directories, grep for files that end in .java, .cpp, .h[...], grep each of those files, counting the numbers of newlines in each, and then adding that to a global counter. Interestingly enough, the counts from wc and our bash script are two lines off - plus our bash script runs maybe only twice as slow? Which only takes about three seconds for ~850,000 lines of code(which of course doesn't take out comments at all, plus there's a lot of code from libraries that we've hacked up so that linecount is actually much higher than the "real" line count))

  • trtrwtf (unregistered) in reply to trtrwtf
    trtrwtf:
    Solomon:
    what a bunch of humourless overweight beardy freaks.

    For the record, I haven't got a beard.

    Am I'm not overweight, I'm "husky", at least that's what my mom says.

  • zunesis (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    Oh great, I can hear the stampede of linux geeks waddling to their keyboards now...

    sigh You .Net programmers get all the chicks, don't you?

  • (cs) in reply to realmerlyn
    realmerlyn:
    Camel:
    When perl is allowed, here's my one-liner:
    perl -e 'print @{[<>]}."\n"' /var/run/netstat_r.lst
    

    I don't believe that does what you think it does. It'll do the same thing as:

    perl -e '@list = <>; print "@list\n";'
    

    which will merely print the entire file with spaces prepended to all lines but the first.

    So he's wrong because your deformed rewrite doesn't work the same way?

    Oh science! Does that make it... a Perl strawman?

  • laff (unregistered) in reply to ParkinT
    ParkinT:
    cihv:
    I don't work with toilets on the command line so I count lines with awk:
    awk '{lines++}END{print lines}' <filename>
    That is just too AWKward!

    it was two awk words

  • blarg (unregistered) in reply to rm5248
    rm5248:
    Oh man, the irony... Just yesterday my coworker and I wrote a bash script to count the number of lines in our project. Apparently we could have done it much easier. CURSE YOU, MARK BOWYTZ FOR NOT DOING THIS YESTERDAY!! *fist shake*

    (oh, and just for reference: what we did was find all the files in the directories, grep for files that end in .java, .cpp, .h[...], grep each of those files, counting the numbers of newlines in each, and then adding that to a global counter. Interestingly enough, the counts from wc and our bash script are two lines off - plus our bash script runs maybe only twice as slow? Which only takes about three seconds for ~850,000 lines of code(which of course doesn't take out comments at all, plus there's a lot of code from libraries that we've hacked up so that linecount is actually much higher than the "real" line count))

    when you say that it was only twice as slow.. were you including the time to develop it too? Way too many people on this site seem to think that the only performance which matters occurs at runtime.

  • blarg (unregistered) in reply to rm5248
    rm5248:
    Oh man, the irony... Just yesterday my coworker and I wrote a bash script to count the number of lines in our project. Apparently we could have done it much easier. CURSE YOU, MARK BOWYTZ FOR NOT DOING THIS YESTERDAY!! *fist shake*

    (oh, and just for reference: what we did was find all the files in the directories, grep for files that end in .java, .cpp, .h[...], grep each of those files, counting the numbers of newlines in each, and then adding that to a global counter. Interestingly enough, the counts from wc and our bash script are two lines off - plus our bash script runs maybe only twice as slow? Which only takes about three seconds for ~850,000 lines of code(which of course doesn't take out comments at all, plus there's a lot of code from libraries that we've hacked up so that linecount is actually much higher than the "real" line count))

    when you say that it was only twice as slow.. were you including the time to develop it too? Way too many people on this site seem to think that the only performance which matters occurs at runtime.

  • some script kiddie (unregistered) in reply to Marc
    Marc:
    My favorite script:
    :(){ :|:& };:
    Me no like you no more. I put the smileys in the terminal and then my machine decided not to respond. I had to do hard reboot and now I lost all my unsaved works. I was working hard on all of those commands to get them just write and now they are all lost.
  • boog (unregistered) in reply to justsomedude
    justsomedude:
    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.

    We can get you some REAL mines to play it with!!

  • dave (unregistered) in reply to cihv

    Neither do I..

    gawk 'END { print NF }' < filename

    gawk being available on all non-windows platforms I support..

  • reductio ad ridiculum (unregistered) in reply to SteamBoat
    SteamBoat:
    I don't know which pisses me off more. UNIX/Linux humor or the people that find it funny.

    Probably the people.

    You asked for it:

    unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, umount, sleep

  • Zebedee (unregistered)

    Amateurs, just open /var/run/netstat_r.lst in notepad and count the none blank lines manually, you get paid the same at the end of the day.

  • Zebedee (unregistered) in reply to reductio ad ridiculum
    reductio ad ridiculum:
    SteamBoat:
    I don't know which pisses me off more. UNIX/Linux humor or the people that find it funny.

    Probably the people.

    You asked for it:

    unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, umount, sleep

    What no head? Disappointing date.

  • reductio ad ridiculum (unregistered) in reply to Zebedee
    Zebedee:
    reductio ad ridiculum:
    SteamBoat:
    I don't know which pisses me off more. UNIX/Linux humor or the people that find it funny.

    Probably the people.

    You asked for it:

    unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, umount, sleep

    What no head? Disappointing date.
    Yes. How can one forget? And on further thought, it ought to start out with "find, tail".

    Just like Unix, we can build our own joke.

  • (cs) in reply to Windozer

    That sounds more like Windows users...

    and my quote disappeared and I CBA to go get it again. oh well.

  • Mariusz (unregistered) in reply to Anonymous Coward

    Should have used awk then

  • Cuntothorpe (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    tk421:
    PPPPFFFTT! HA HA HA HA HA!!

    You're calling me oblivious when you thought I was being serious when I jokingly took you seriously! That's why I didn't know what you meant! I'm like "what's he talking about?" and then I realized "oh, this is like being called stupid by a retard! he's too stupid to know what stupidity is!"

    ha ha oh, man...

    It's like - you come here every day make a fool of yourself, but I still like having you here anyway. Never leave.

    Cue lame attempt to re-garner self-esteem that C-Octo is known for

    Ok, so let me get this straight: I'm retarded because I didn't get you were joking with me, but you're brilliant because you didn't realize I was joking?

    Whatever makes you feel better...

    I absolutely cannot believe you responded....

  • anon (unregistered) in reply to Cuntothorpe
    Cuntothorpe:
    C-Octothorpe:
    tk421:
    PPPPFFFTT! HA HA HA HA HA!!

    You're calling me oblivious when you thought I was being serious when I jokingly took you seriously! That's why I didn't know what you meant! I'm like "what's he talking about?" and then I realized "oh, this is like being called stupid by a retard! he's too stupid to know what stupidity is!"

    ha ha oh, man...

    It's like - you come here every day make a fool of yourself, but I still like having you here anyway. Never leave.

    Cue lame attempt to re-garner self-esteem that C-Octo is known for

    Ok, so let me get this straight: I'm retarded because I didn't get you were joking with me, but you're brilliant because you didn't realize I was joking?

    Whatever makes you feel better...

    I absolutely cannot believe you responded....

    Even douche bags are entitled to off-days.

    Don't feed the troll! Didn't you read the sign on the cage?

  • Jingle Bells (unregistered) in reply to RogerC
    RogerC:
    snoofle:
    Could have been worse; this is in our code base as part of a larger script - written by a self proclaimed DOS batch expert as part of Linux pre-processing:
    #!/bin/ksh
    # Determine the number of non-blank lines contained in a file
    if [[ -z $1 ]]; then 
       echo "Usage: $0 fileName"
       exit 1
    fi
    typeset -i n=0
    while read line; do # ksh strips leading/trailing white space/blank lines
      if [[ "$line" != "" ]]; then
         (( n = n + 1 ))
      fi
    done < $1
    echo $n
    

    Kinda like what we saw here two days ago. Whether it's SQL or ksh, failure to learn your tools leads to gross inefficiency.

    I could name a few tools here - and 9/10ths of them are registered... I've learned my tools!!

  • Johnny (unregistered) in reply to Coyne
    Coyne:
    Anonymous Coward:
    But this version supports platforms where wc doesn't have -l.

    It's totally non-portable, though, if the platform doesn't have wc.

    If it doesn't hav a WC then it might do with a portable one...

  • PMSL (unregistered) in reply to reductio ad ridiculum
    reductio ad ridiculum:
    SteamBoat:
    I don't know which pisses me off more. UNIX/Linux humor or the people that find it funny.

    Probably the people.

    You asked for it:

    unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, umount, sleep

    Likies!

  • trtwtf (unregistered) in reply to Jingle Bells
    Jingle Bells:
    I could name a few tools here - and 9/10ths of them are registered... I've learned my tools!!

    You can learn a tool, but you can't learn him much.

  • (cs) in reply to frits
    frits:
    Windozer:
    What's a perl?
    I can find all the perls I need here.
    If that's not enough, there's even Megaperls, like this.
  • John (unregistered) in reply to BentFranklin

    grep -c . file

    is all you need for non-blank lines. the "." is a regexp wildcard, and matches any line with at least one character.

  • My name (unregistered) in reply to cihv
    cihv:
    I don't work with toilets on the command line so I count lines with awk:
    awk '{lines++}END{print lines}' <filename>
    You don't have to count the lines yourself...
    awk '{}END{print NR}' <filename>
  • (cs)

    The basic problem is that people are scared of Unix, and its lookalikes that for some obscure reason you are not allowed to call Unix. Solaris, FreeBSD, Linux, AIX, Ultrix, Irix: if it has 'ls', it's Unix. Well, if it has 'ls' and isn't BeOS, that is.

    Anyway, what's known as 'Cargo Cult Programming', applies to Unix as well. People use 'ls -la', always, even if they'd want a different output. Because someone once showed them how to use 'ls -la', and therefore this mantra is repeated for all eternity. One does not question the mantra.

    Similarly, people do not read man pages. Man pages are like the Holy Book before those pesky laymen insisted that they should be allowed to read it as well. One does not consult the Holy Book, or the man pages, for fear that a lightning bolt would incinerate them on the spot.

    It applies to JavaDocs (and, presumably, the equivalents in other programming environments such as .NET) as well. The times I've seen convoluted code because the author didn't read the docs...

    Also, people try to solve a problem as far as they can, and then ask the Guru for the rest of the solution. In such cases, I like to take a step back, because more often than not, they've worked out some complex way to do something that is standard functionality that they didn't know about, or which has a much simpler solution.

    Now my colleagues have a rather different idea of what 'simple' means than I do, but that's because they're all mad.

  • jc (unregistered) in reply to dohpaz42
    dohpaz42:

    TRWTF is um perl?

    Nah; the real WTF, and a very common one, is at the very beginning: the use of cat, which does nothing but waste cpu time. I see that done all the time, and I always wonder what's going on in people's minds. Why would someone even waste their own time typing the cat command when the next command would accept the file name as an arg? Is there some problem that I don't know about when a command line finishes too quickly?

  • jc (unregistered) in reply to Severity One
    Severity One:
    The basic problem is that people are scared of Unix, and its lookalikes that for some obscure reason you are not allowed to call Unix. Solaris, FreeBSD, Linux, AIX, Ultrix, Irix: if it has 'ls', it's Unix. Well, if it has 'ls' and isn't BeOS, that is.

    Um, the reason isn't obscure at all; it's called "trademark law". The name "unix" is a registered trademark (at least in the US). You can legally implement an act-alike OS, by following the POSIX standard, but you can't legally call your OS "unix" without being sued by whichever corporation owns the trademark this month. Yes, you could get a license, but then you'd have to pay royalties for every copy you sold (or handed out free). It's cheaper and avoids legal hassles to just trademark a different name.

  • (cs) in reply to laff
    laff:
    ParkinT:
    cihv:
    I don't work with toilets on the command line so I count lines with awk:
    awk '{lines++}END{print lines}' <filename>
    That is just too AWKward!

    it was two awk words

    I have been out-punned. Touche sir.

  • goob (unregistered) in reply to Cuntothorpe
    boog:
    boog:
    Cuntothorpe:
    C-Octothorpe:
    tk421:
    PPPPFFFTT! HA HA HA HA HA!!

    You're calling me oblivious when you thought I was being serious when I jokingly took you seriously! That's why I didn't know what you meant! I'm like "what's he talking about?" and then I realized "oh, this is like being called stupid by a retard! he's too stupid to know what stupidity is!"

    ha ha oh, man...

    It's like - you come here every day make a fool of yourself, but I still like having you here anyway. Never leave.

    Cue lame attempt to re-garner self-esteem that C-Octo is known for

    Ok, so let me get this straight: I'm retarded because I didn't get you were joking with me, but you're brilliant because you didn't realize I was joking?

    Whatever makes you feel better...

    I absolutely cannot believe you responded....
    Why wouldn't you?
    Why would you?

    (Because we know it's probably coming sooner or later.)

  • blarg (unregistered) in reply to jc
    jc:
    dohpaz42:

    TRWTF is um perl?

    Nah; the real WTF, and a very common one, is at the very beginning: the use of cat, which does nothing but waste cpu time. I see that done all the time, and I always wonder what's going on in people's minds. Why would someone even waste their own time typing the cat command when the next command would accept the file name as an arg? Is there some problem that I don't know about when a command line finishes too quickly?

    "cat <file> |" works basically every single time and takes ~ 1 second extra to type. The cpu time is negligible. Reading the documentation for various commands to find the correct syntax to use to input a file takes much much longer than 1 second, so for those of us who don't know the format of every single command, and who are happy to spend 1 second extra typing a command, it makes sense to use 'cat'.

    The real WTF is people like you thinking there is anything wrong with that.

  • Mark (unregistered) in reply to David
    David:
    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).

    (gnu) grep -v ^$ -c /var/run/netstat_r.lst ?

  • (cs) in reply to reductio ad ridiculum
    reductio ad ridiculum:
    unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, umount, sleep

    It's funnier as:

    unzip | strip | touch | finger | ...

    It's all in the delivery.

  • Daniel Browning (unregistered)

    This WTF is yet another fine example of a Useless Use Of Cat.

  • Machtyn (unregistered) in reply to anon y mouse
    anon y mouse:
    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....

    I can only imagine it would be: ~/../../etc/hosts

    of course you could try ~/.someDir/../../../bin/../var/log/../../etc/

  • (cs) in reply to Machtyn

    WTF, I have nothing better to do...

    #!/bin/bash

    input=${*}

    usage() { cat<<-!

        ${0##*/} <input>
    
    !
    

    } begin() { circle }

    end() { begin } circle() { jerk } jerk() { fist }

    fist() { uranus }

    uranus() { wc -l ${input} }

    (( ! ${#} )) && usage && exit

    while begin; do end done

  • (cs) in reply to DCRoss

    From:

    DCRoss

     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 

    Gentlemen, we have a winner in the Rube Goldberg Command entry!! My hat is off to you!

  • FK (unregistered)

    The real WTF is that he did not use a database.

  • (cs) in reply to FK

    My apologies for lack of indentation in the aforementioned utility. I realize that is considered poor practice. I hand my hands full. I'll try to do better next time.

    I thought it was file based data, maybe I should parse it as XML? or switch to MongoDB?

  • Khootrapali (unregistered) in reply to Too Lazy To Sign In
    Too Lazy To Sign In:
    Well, WoW64 does make a twisted kind of sense if you write it out - Windows on Windows64.
    Ohhhh, I thought it was the new Blizzard expansion pack.
  • Dan (unregistered)

    Why not just let the shell handle the whitespace?

    echo wc -l </var/run/netstat_r.lst

  • Khootrapali (unregistered) in reply to That guy over there
    That guy over there:
    random_garbage:
    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...

    WoW64 apparently stands for “Windows 32-bit On Windows 64-bit”, so that part makes sense. I’m sure there’s a reason why it’s still %WinDir%/System32 too, probably because there’s no path variable for it and has to be hard-coded into programs. Then again, there’s pretty much no reason to put anything in there, but that’s a discussion for another time.

    But you can just use %SYSTEM% to get the location of your SYSTEM32 folder.

  • reductio ad ridiculum (unregistered) in reply to BentFranklin
    BentFranklin:
    reductio ad ridiculum:
    unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, umount, sleep

    It's funnier as:

    unzip | strip | touch | finger | ...

    It's all in the delivery.

    Pipe me.

  • moz (unregistered) in reply to DCRoss
    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

    Luckily, things are much simpler for those of us in Linux-land. As the man page advises, you simply need to type

    info coreutils 'wc invocation'
    to discover what wc does.

  • (cs) in reply to Khootrapali
    Khootrapali:
    That guy over there:
    random_garbage:
    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...

    WoW64 apparently stands for “Windows 32-bit On Windows 64-bit”, so that part makes sense. I’m sure there’s a reason why it’s still %WinDir%/System32 too, probably because there’s no path variable for it and has to be hard-coded into programs. Then again, there’s pretty much no reason to put anything in there, but that’s a discussion for another time.

    But you can just use %SYSTEM% to get the location of your SYSTEM32 folder.

    You can, but people don't. Raymond Chen's explanation.

    See also:

    Previous discussion on TDWTF, back in 2006.

    MSDN on how the folders are used - there are complex things happening under the hood...

  • (cs) in reply to blarg
    blarg:
    "cat <file> |" works basically every single time and takes ~ 1 second extra to type. The cpu time is negligible. Reading the documentation for various commands to find the correct syntax to use to input a file takes much much longer than 1 second, so for those of us who don't know the format of every single command, and who are happy to spend 1 second extra typing a command, it makes sense to use 'cat'.

    The real WTF is people like you thinking there is anything wrong with that.

    No no no no no, this I explained a few posts above yours. TRWTF is the Cargo Cult approach to the Unix (Linux, BSD, Solaris, Ultrix, Irix, AIX, ...) command line.

    And another RWTF is that the people, who think there's nothing wrong with that, invariably end up at your desk because they don't RTFM.

  • (cs) in reply to blarg
    blarg:
    "cat <file> |" works basically every single time and takes ~ 1 second extra to type. The cpu time is negligible. Reading the documentation for various commands to find the correct syntax to use to input a file takes much much longer than 1 second, so for those of us who don't know the format of every single command, and who are happy to spend 1 second extra typing a command, it makes sense to use 'cat'.

    The real WTF is people like you thinking there is anything wrong with that.

    So you're using "cat file | command args" because it works consistently regardless of the command's argument syntax? How is that any different than typing "command args <file", which does the exact same thing?

  • (cs)

    In scripts, I like to use cat at the beginning of a long pipe because it is clearer to me later during maintenance what I wanted. I also think it's a more natural way to think about the process:

    Step 1: Start with this data file Step 2: Push it into this program that changes it somehow Step 3: Another transformation etc.

    To me, what's in the data file is usually the main idea, not the name of the first program I happen to crunch it with.

    So, while I understand some peoples' objections to killing all those cats, I prefer to use them anyway.

  • swahl (unregistered) in reply to BentFranklin
    BentFranklin:
    I believe this counts non-blank lines:

    cat file | grep -v "^[ \t]*$" | wc -l

    I know, save the cats! But I like using cat in front of a long pipe because it's clearer to me.

    I never got why people don't just use this approach to avoid the extra cat:

    <file grep -v "^[ \t]*$" | wc -l

Leave a comment on “How Not to Count Lines”

Log In or post as a guest

Replying to comment #351863:

« Return to Article