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 > need help in modifying the contents of a text file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-04, 15:11
akk8q8 akk8q8 is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
need help in modifying the contents of a text file

Hello,
I am a novice in shell scripting. Need help in modifying the contents of a text file.

the contents of the text file are:
[foldername.session name]
$$deptno=10

I want to change the value $$deptno=40. Cud any one suggest a shell script which prompts the user for the value and updates deptno with the value.

Finally ...the shell script every time its run it sud modify the value of $$deptno as defined by the user.

Thanking you,
ak
Reply With Quote
  #2 (permalink)  
Old 02-23-04, 15:35
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try and adapt the following code :

Code:
#
# Read new deptno value
#
while [ -z "$deptno" ]
do
   echo "Enter new deptno value :"
   read deptno || exit 1
done
#
# Modify $$deptno line
#
sed -e 's/^$$deptno=.*/$$deptno='$deptno'/' xyz.txt > xyz.txt.$$
mv xyz.txt.$$ xyz.txt
The 'echo/read' sequence can be replaced by one read statement depending on the shell used.
Code:
# Ksh
   read deptno?"Enter new deptno value : " || exit 1

# Bash
   read -p "Enter new deptno value : " deptno || exit 1
The 'sed/mv' sequence can be replaced by one 'perl' statement
Code:
perl -p -i -e 's/\$\$deptno=.*/\$\$deptno='$deptno'/' xyz.txt
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 02-23-04, 18:10
akk8q8 akk8q8 is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
Re: need help in modifying the contents of a text file

perl -p -i -e 's/\$\$deptno=.*/\$\$deptno='$deptno'/' xyz.txt

cud u please explain wht -p -i and -e stand for.

And also while accepting deptno from the user why is it in a while loop?

Thanks
ak







Quote:
Originally posted by akk8q8
Hello,
I am a novice in shell scripting. Need help in modifying the contents of a text file.

the contents of the text file are:
[foldername.session name]
$$deptno=10

I want to change the value $$deptno=40. Cud any one suggest a shell script which prompts the user for the value and updates deptno with the value.

Finally ...the shell script every time its run it sud modify the value of $$deptno as defined by the user.

Thanking you,
ak
Reply With Quote
  #4 (permalink)  
Old 02-23-04, 18:12
akk8q8 akk8q8 is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
hello Jean-Pierre,

Cud explain wht p , i, e stand for in the sed command.
Also why do we have to use a while loop for accepting deptno

thanks,
ak
Reply With Quote
  #5 (permalink)  
Old 02-23-04, 18:40
akk8q8 akk8q8 is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
The script works perfectly.Thanks
Reply With Quote
  #6 (permalink)  
Old 02-24-04, 01:43
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Its a 'perl' command (not 'sed').

Quote:
Usage: perl [switches] [--] [programfile] [arguments]
-0[octal] specify record separator (\0, if no argument)
-a autosplit mode with -n or -p (splits $_ into @F)
-c check syntax only (runs BEGIN and END blocks)
-d[:debugger] run scripts under debugger
-D[number/list]set debugging flags (argument is a bit mask or flags)
-e 'command' one line of script. Several -e's allowed. Omit [programfile].
-F/pattern/ split() pattern for autosplit (-a). The //'s are optional.
-i[extension] edit <> files in place (make backup if extension supplied)
-Idirectory specify @INC/#include directory (may be used more than once)
-l[octal] enable line ending processing, specifies line terminator
-[mM][-]module.. executes `use/no module...' before executing your script.
-n assume 'while (<>) { ... }' loop around your script
-p assume loop like -n but print line also like sed
-P run script through C preprocessor before compilation
-s enable some switch parsing for switches after script name
-S look for the script using PATH environment variable
-T turn on tainting checks
-u dump core after parsing script
-U allow unsafe operations
-v print version number, patchlevel plus VERY IMPORTANT perl info
-V[:variable] print perl configuration information
-w TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT. Recommended.
-x[directory] strip off text before #!perl line and perhaps cd to directory
The loop ask for deptno until a non null reponse is given.
__________________
Jean-Pierre.
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