Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Posts
    3

    Unanswered: How to subtitute a field value and save it in the same file

    I have a korne/bourne shell script which fits in an existing script and collects data for multiple users.
    I could write the scripts with one output to one file which has the data collection. The program is that it appends the file each time the script runs.
    Note :
    The scipt is all about collecting no. of pages printed by each users, each time user prints a job it has to collect the no. of pages and add it to field for the existing user entry , instead of having an additional entry for the same user.

    The Printing job script already exists and functional.
    --------------------------------------------------------------
    # This program will collect the no_of_pages data everytime the user successfully queues his print jobs
    # The program will run after user print request is queued
    # This script should be added after print spooling command

    pusr=`whoami`
    ls | grep pusg
    if [ $? != 0 ]
    then
    echo "$pusr $no_of_pages" > pusg
    chmod 777 pusg
    touch npt
    chmod 777 npt
    else
    cat pusg |grep -w $pusr
    if [ $? = 0 ]
    then
    npctr=`grep $pusr pusg |awk '{print $2}' |sort -n |tail -1`
    npctr=`expr "$no_of_pages" + "$npctr"`
    echo "$pusr $npctr" >> pusg
    sort -nr +1 pusg > npt
    else
    echo "$pusr $no_of_pages" >> pusg
    fi
    fi
    --------------------------------------------------------------
    The pusg file gets appended everytime even if the user entry already exists in this file.

    Please suggest me the way to write the changes in the second field of the entry for the existing user without adding one more entry as it does in my script (echo "$pusr $npctr").

    Secondly let me know can I apply pusg file location and permissions so that users only use it within the script and should not tamper it.

    Expecting your kind help ASAP.

    thank
    Ashraf

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    you can do something like this to EDIT the content of the file substituting the old number with the new one:

    ex ${pusg} <<EOF
    s/${pusr}.*/${pusr} ${npctr}/g
    wq!
    EOF

  3. #3
    Join Date
    Apr 2004
    Posts
    3

    file field substitution

    I tried your solution and it works independly after testing.
    However it does not work within the script....
    it gives error message
    ---------------------------------------------------
    Syntax error at line 15 : `<' is not matched.
    ---------------------------------------------------
    This line is where the ex section starts

    Hoping for more help from your side....

    Thanks

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    make sure that '<<EOF' have no spaces following it AND the matching 'EOF' has no spaces preceeding it. In other words, validate that you're usig the solution as posted.

  5. #5
    Join Date
    Apr 2004
    Posts
    3

    file field substitution

    it works even within the script if I comment the if statement and its portion.....
    In other words it is working without including if command in my script, which will not server my purpose as this script should be able to run for multiple users who use our printing system.
    (BTW there is no syntax error when I run script without if statements)

    Please suggest further work-around....ASAP...


    Ashraf

Posting Permissions

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