Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2005
    Posts
    6

    Unanswered: Bourne Shell scripting & awk help

    -File -

    // This program was written by Kevin and Ali
    public void main() {
    int x=0; // x is the x coordinate

    -end File -

    I am trying list all the comments (with line numbers) in the file above.

    For simplicity, all coments start with // and there is a space before and after the slashes. My problem is not listing the comments with the line numbers, it is that I do not want to list the entire line, just whatever is after the // token. For example if I have:

    x = 0; // x is equal to zero

    if use grep -n " //" $2 , then it lists the whole line:

    1: x = 0; // x is equal to zero

    Instead I would like it to list everything after the comment like this:

    1: x is equal to zero

    After looking around I realize that grep does not allow me to manipulate the data as I please so I will have to use awk.

    I tried using awk but have had little success. If anyone can please help with using awk with this problem I am very greateful.

    Thank you in advance for all replies.

    -Sweetness

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    nawk 'match($0, "//") { printf("%d : %s\n", FNR, substr($0, RSTART+2)); }' file.C
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    or with sed [not pretty]:

    sed -n '/\/\//{=;s/\(.*\)\/\/\(.*\)/\2/p;}' file.C | sed 'N;s/\n/ : /'
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  4. #4
    Join Date
    Jan 2005
    Posts
    6

    Thank you vlad

    Vlad-

    Thank you very much for yout help, it was much more than I expected.

    -Sweetness

  5. #5
    Join Date
    Jan 2005
    Posts
    6

    Also...

    Vlad -

    If you dont mind me asking where can I look for commands such as %d to print the line, so I may use as a reference later on.

    Thank you.

    -Sweetness

  6. #6
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    '%d' is a formating directive - not really a command per se.
    If you want to find out the details about other format directive, you can do 'man -s 3S printf' [well, at least on Solaris].

    If you're after something else, pls clarify.

    vlad
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  7. #7
    Join Date
    Jan 2005
    Posts
    6

    Another issue

    grep -n $3 $2

    echo "Which line would you like to edit (integer value 1,2,3...):"
    read line
    echo "You want to edit line $line"
    echo "What would you like to use for the replacement text:"
    read repText

    sed "s/$3/$repText/g" $2 > temp1
    cat temp1 > file
    sed "$line!d" file ;;

    --------------------------------------------------------

    What I am trying to do with the above code is search for a certain 'text' in a file and print every line that contains the 'text' with the line number. After printing I prompt the user to choose which line to edit. I would like to be able to change that 'text' with the 'newText' in the line the user chooses.

    My problem is trying to isolate a particular line in the file becuase I am not sure as to where the line number is stored. Also I want the file to be permanently saved.

    Thank you for any help.

    -Sweetness

  8. #8
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    something like this to begin with - iwth no error checking:

    sweet.sh file.txt "myPattern2change"

    here's sweet.sh
    Code:
    #!/bin/ksh
    file="${1}"
    pat="${2}"
    
    grep -n "${pat}" "${file}"
    
    echo "Which line would you like to edit (integer value 1,2,3...):"
    read line
    echo "You want to edit line ${line}"
    echo "What would you like to use for the replacement text:"
    read repText
    
    ex - "${file}" <<EOF
    ${line}s/${pat}/${repText}/g
    wq!
    EOF
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

Posting Permissions

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