Results 1 to 2 of 2

Thread: Can someone help me with this shell scripting prob

  1. #1

    Can someone help me with this shell scripting prob

    Hi, I'm fairly new at shell scripting and I'm trying to create a small menu system for someone. It's a small script that is designed to analyse a file containing some text. (using Linux, Mandrake 7.2 to be precise).

    I'm currently stuck on the following items:

    1) I'm trying to get a specific letter count. For example, if someone wanted to count the number of 'f' in a text file, how would I do that.

    2) How do I do aword count that can be specified, for example 'milk' in a text file.

    3) A sentence count.

    4) Add an extra line to the text

    5) Replace a word in a text

    I know it has something to do with grep, sed and wc, but I can't get around to piping them correctly.

    Just one other thing, How do I get a menu system to work? I currently have a menu system that you can chose options such as:

    [1] Open file
    [2] List File
    [3] Word count

    I did this using functions but after the menu function has been executed once, then I call it from another function it doesn't seem to work, this is how my structure is:

    function menu
    menu code
    option1 #this line calls the function option

    menu #this line calls the function menu above

    function option1
    Option code

    menu # this line is the problem, it calls the function, but the menu function doesn't work properly


    Thank you for any help

  2. #2

    Re: Can someone help me with this shell scripting

    I don't know if you're still looking for answers to this, but I thought I'd have a bash anyway (no pun intended :).

    To count the f's in myfile.txt
    sed -e 's/f/f\
    /g' myfile.txt |grep --count f
    grep counts only lines, so the sed replaces all f's with f followed by newline (the new line in that sequence is quite deliberate). I guess if you research a bit, you could probably come up with something less clumsy

    sed -e 's/\bmilk\b/\0\
    /g' myfile.txt |grep --count "\bmilk\b"
    A sentence count might be (probably some flaws in my thinking here but what the hell)
    sed -e 's/[\.\?\!]+/\0\
    /g' myfile.txt |grep --count "[\.\?\!]"
    To assign the output from a command such as those above to a variable in your script, you need to surround the sequence with the backtick (`) character.

    To add an extra line you could use:
    cat >>myfile.txt
    To replace a word, modify that sed above (the one that replaces milk with milk newline).

    As for your menu function, I can only assume bash doesn't like recursion much. One way around it would be:
    function menu
    while :
    <your menu code>
    This will create a never ending loop. Of course you could add a quit option and put it in as a condition to the while loop.

    Edit: corrected half baked sentence count (a bit)

Similar Threads

  1. Some shell scripting links
    By Outlaw in forum Linux - Software, Applications & Programming
    Replies: 7
    Last Post: 03-19-2010, 05:20 AM
  2. Good shell scripting book
    By tech291083 in forum Redhat / Fedora
    Replies: 2
    Last Post: 05-13-2007, 01:01 PM
  3. GUI Scripting
    By friskydrifter in forum Programming
    Replies: 3
    Last Post: 03-07-2006, 12:08 AM
  4. shell scripting question in hpux11
    By in forum Programming
    Replies: 0
    Last Post: 02-04-2003, 05:51 PM
  5. Korn Shell Scripting
    By NeoStarO1 in forum Programming
    Replies: 11
    Last Post: 08-15-2002, 08:50 PM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts