[Homework?] Complete NOOB need help


Results 1 to 14 of 14

Thread: [Homework?] Complete NOOB need help

  1. #1
    Join Date
    Jun 2007
    Location
    Croydon, Surrey, UK
    Posts
    3

    Complete NOOB need help

    I am a complete NOOB to linux and shell scripting and need help

    this is what I am trying todo

    1. Create a script that takes 1 argument being a file read the inputted file, and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.

    my efforts sofar

    #!/bin/bash
    echo "filename"
    read myvar
    grep $USER $myvar > results.txt
    nl results.txt


    2. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the middle (middle line) of the file named in second argument.

    Thanks in advance for any help
    Last edited by gham; 06-06-2007 at 03:36 AM.

  2. #2
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Hi,

    Have a read of the Posting Guidelines.

    This isn't home work is it

    Have a look at The Linux Documentation Project:

    TLDP : Guides
    Bash Guide for Beginners
    Advanced Bash-scripting Guide

    To see if they can help you at all.


    Your very close to having the first one working properly, '>' redirects and overwrites a file, as you've found out. Have a look through the bash guides, or a google for 'bash file redirection'

    One thing you might think about doing:

    read your results file, and loop through each line, formatting the output the way you want and redirecting it to another file.
    Last edited by deathadder; 06-05-2007 at 08:35 AM.
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  3. #3
    Join Date
    Jun 2007
    Location
    Croydon, Surrey, UK
    Posts
    3
    thanks for the reply ... and NO it's not homework ...

  4. #4
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Had to ask, the number of homework questions is quiet large at times, and to be fair it sounds like one

    I don't think you can format the output of nl the way you want. A while statement is possibly your best way to go...
    Code:
    count = 1
    while read line from results.txt do
         print $count) $line > anotherfile.txt
         count++
    done
    mv anotherfile.txt results.txt
    Last edited by deathadder; 06-05-2007 at 08:52 AM.
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  5. #5
    Join Date
    Jun 2003
    Posts
    173
    This should do close to what you need it to for the first part:
    Code:
    sed "s/$USER/[&]/g" $myvar | grep $USER | nl >results.txt

  6. #6
    Join Date
    Apr 2006
    Posts
    32
    well, the way you ask the questions, it is 99.99% homework to me..but nevertheless there's still 0.01% benefit of the doubt i can give to you.

    Quote Originally Posted by gham
    1. Create a script that takes 1 argument being a file read the inputted file, and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.
    Code:
    USER=$1 #first argument
    awk '/'"$USER"'/{print NR " " $0 > "results" }' "file"
    it says take the input "file", match the user and bring the record number, and that matched line into "results" file.

    2. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the middle (middle line) of the file named in second argument.
    if your file is not very large..this is one way..
    Code:
    awk -v text="$1" -v file="$2" '{arr[++c]=$0}
         END{
            for(i=1;i<=c/2;i++){
              print arr[i] > file
            }
            print text > file
            for(i=c/2+1;i<=c;i++) {
              print arr[i] > file
            }
         }' "file"
    Last edited by ghostdog74; 06-05-2007 at 12:12 PM.

  7. #7
    Join Date
    Jun 2007
    Location
    Croydon, Surrey, UK
    Posts
    3

    Thanks guys

    yep these all work.... except I cannot use AWK or SED .

    and as I have said this is NOT homework .... it's an Adult education course I am doing.
    Last edited by gham; 06-05-2007 at 01:09 PM.

  8. #8
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Wouldn't that make it homework

    Why can't you use awk and sed?
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  9. #9
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Code:
    #!/bin/bash
    echo "filename"
    read myvar
    grep $USER $myvar > results.txt
    x=1
    cat results.txt | while read line
    do
       echo "$x) $line" >>results2.txt
       x=$((x+1))
    done
    Should do what you need, it's not nice though, without using sed or awk.
    Last edited by deathadder; 06-05-2007 at 01:48 PM.
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  10. #10
    Join Date
    Jan 2004
    Location
    Singapore
    Posts
    355
    well... its still homework in a way right? just that its not going graded maybe?
    Registered Linux User #388117

  11. #11
    Join Date
    Sep 1999
    Posts
    3,202
    At least I know you aren't one of my students... we don't start shell scripting until tonite's class

  12. #12
    Join Date
    Apr 2006
    Posts
    32
    Quote Originally Posted by deathadder
    Code:
    #!/bin/bash
    echo "filename"
    read myvar
    grep $USER $myvar > results.txt
    x=1
    cat results.txt | while read line
    do
       echo "$x) $line" >>results2.txt
       x=$((x+1))
    done
    Should do what you need, it's not nice though, without using sed or awk.
    catting a file and piping to while loop is unnecessary because the while loop itself can take in redirection. it can be shortened to
    Code:
    while read line 
    do
    ....
    done < $myvar
    or just
    Code:
    ...
    grep $USER $myvar | while read line 
    ....

  13. #13
    Join Date
    Oct 2005
    Location
    Nuernberg, Germany
    Posts
    183
    why not have a look at fgrep?? That should trim it down to about 2-3 line of code

  14. #14
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Ah thanks for letting me know that ghostdog74, my scripting is in need of improvement as you can tell
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

Posting Permissions

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