Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2004
    Posts
    5

    Unanswered: 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

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  3. #3
    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







    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

  4. #4
    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

  5. #5
    Join Date
    Feb 2004
    Posts
    5
    The script works perfectly.Thanks

  6. #6
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Its a 'perl' command (not 'sed').

    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.

Posting Permissions

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