Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help for an exam today@9am! (Linux command line)

Options
  • 19-03-2008 2:18am
    #1
    Registered Users Posts: 7,029 ✭✭✭


    Hi all , on the off chance some one sees this before 9am today!

    I need to know two ways of re-writing the following( for added fun i may have taken it down wrong!)
    Cat  readme.txt|grep mix| wc-l
    

    All i can come up with is
    grep mix <readme.txt> tmp
    wc-l <tmp
    

    Any help greatly appredciated and will most certainly get my thanks!post_thanks.gif


Comments

  • Registered Users Posts: 868 ✭✭✭brianmc


    Hi all , on the off chance some one sees this before 9am today!

    I need to know two ways of re-writing the following( for added fun i may have taken it down wrong!)
    Cat  readme.txt|grep mix| wc-l
    

    All i can come up with is
    grep mix <readme.txt> tmp
    wc-l <tmp
    

    Any help greatly appredciated and will most certainly get my thanks!post_thanks.gif


    Well, since it's too late now, I'll go int plenty of detail.

    The command line should be...
    cat readme.txt | grep mix | wc -l
    

    Pretty much everything in Linux is case sensitive.

    "cat" doesn't do much. It takes input - either from the terminal (i.e. you type it) or it reads from a file (as above with "cat readme.txt"). All cat does with it's input is output it.

    The pipe symbol "|" simply takes the ouput of a program and makes it the input for another program that would otherwise read from the terminal - in the first instance here - "grep".

    grep can be used a lot of ways. The most siginificant ones in this case are you can give it a pattern and a filename to read from and it will (unsurprisingly) read from the file and look for the pattern. The other thing you can do is give it a pattern only (no filename) and it will try and read from standard input (often the terminal, but in this case from the output of the "cat" command).

    "wc -l" counts lines in it's input text. Like grep you can give it a filename to read from or you can leave the filename out and it will try to read from standard input. In this example the "|" is being used again to make the output of the grep the input for the "wc" command.

    So...

    Since grep can read directly from a file... you could skip the "cat" command and the associated "|". I.E.
    grep mix readme.txt | wc -l
    

    I suspect that that is obvious alternative number one.

    I can't see such an obvious alternative for number two but grep can also count the number of lines that match a pattern instead of printing them out. You can do this with a "-c" option. So there's an alternative number two.
    grep -c mix readme.txt
    

    There are a rake of ways of writing it that are more convoluted and pointless of course.


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Just because I'm feeling sadistic, this should probably work.
     awk 'BEGIN{count =0}; /mix/ {count = count +1}; END{print count}' readme.txt
    

    Basically what it does is set a variable called count, and every line that you hit with max in it will up the linecount, and at the end it will print the linecount.

    The much simpler version is:
    grep -c 'mix' readme.txt
    

    (I initially wrote the awk script because I thought that -c just counted the occurances rather than the number of lines that the pattern occurs in, so it would be different functionality, but apparently not).

    Anyway, welcome to the wild and wacky world of unix command-line. (It's fun once you get the head around it)

    Aoife


Advertisement