If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to subtitute a field value and save it in the same file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-28-04, 09:14
ashrafali ashrafali is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
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
Reply With Quote
  #2 (permalink)  
Old 04-28-04, 14:45
vgersh99 vgersh99 is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 05-01-04, 09:57
ashrafali ashrafali is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 05-01-04, 11:08
vgersh99 vgersh99 is offline
Registered User
 
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.
Reply With Quote
  #5 (permalink)  
Old 05-02-04, 04:54
ashrafali ashrafali is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On