Update a specific line in a file while reading sequentially

All,

I know this is a very naive question but I could not find a way to get this working!

I have a file with values like

input.file

Value1
Value2
server1/mylogin,mypasswd
Value3
Value4

And in my code, I am reading the file line by line and processing it.

#! /bin/ksh
line_no=1;

while read line
do
  case $line_no in
  1) var1=$line;;
  2) var2=$line;;
  3) 
    logon_var=$line
    [I need to add my code here to update this line in the file
    [with masking text "SERVER/USERNAME,PASSWORD"]
  ;;
  4) var3=$line;;
  5) var4=$line;;
  esac

  line_no=`expr $line_no + 1`
done < input.file

echo $logon_var

Once after running the code I should see the file like:

input.file

Value1
Value2
SERVER/USERNAME,PASSWORD
Value3
Value4

Any help is greatly appreciated. Thanks

Bharath

sed 's#server1/mylogin,mypasswd#SERVER/USERNAME,PASSWORD#' input.file

Thanks,

I tried using the variable instead of the text itself like:

mask_text="SERVER/USERNAME,PASSWORD"
sed 's#$line#$mask_text#' input.file

This does not change anything in the input file. Also as part of the output I got the whole input file in the terminal which is kind of breaks everything that I am trying to achieve. I need to mask the logon details with NO one seeing any messages of the same!!!

Also when I give the text itself, it does not update the file rather it shows the updated file contents in the terminal.

Please help!

echo $mask_text
SERVER/USERNAME,PASSWORD

cat abc.txt
Value1
Value2
server1/mylogin,mypasswd
Value3
Value4


sed "s#server1/mylogin,mypasswd#$mask_text#" abc.txt
Value1
Value2
SERVER/USERNAME,PASSWORD
Value3
Value4

If the contents in the file need to be changed use "-i" option

sed -i "s#server1/mylogin,mypasswd#$mask_text#" abc.txt

HTH,
PL

Thanks PL,

I tried executing the sed command you gave and below is the result I get:

Input File

$ cat input.file
Value1
Value2
server1/mylogin,mypasswd
Value3
Value4

Code

$ cat editfile.test
#! /bin/ksh
line_no=1;
mask_text="SERVER/USERNAME,PASSWORD"

while read line
do
  case $line_no in
  1) var1=$line;;
  2) var2=$line;;
  3) 
    logon_var=$line
    #YOUR CODE
    sed "s#server1/mylogin,mypasswd#$mask_text#" input.file [I WANT TO USE $line INSTEAD OF server1/mylogin,mypasswd]
  ;;
  4) var3=$line;;
  5) var4=$line;;
  esac

  line_no=`expr $line_no + 1`
done < input.file

echo $logon_var

Output

$ ./editfile.test
Value1
Value2
$mask_text
Value3
Value4
server1/mylogin,mypasswd [This line is the result of the echo command]

Input file after execution

$ cat input.file
Value1
Value2
server1/mylogin,mypasswd [NO CHANGE, But I want this Changed]
Value3
Value4

Now adding -i flag in the sed, this time I used the same code above except for the sed line for which I used the below:

    #YOUR CODE
    sed -i "s#server1/mylogin,mypasswd#$mask_text#" input.file

Output

$ ./editfile.test
sed: Not a recognized flag: 1 [NOT SURE WHY GOT THESE, I AM USING KSH]
Usage:  sed [-n] Script [File ...]
           sed [-n] [-e Script] ... [-f Script_file] ... [File ...]
server1/mylogin,mypasswd [This line is the result of the echo command]

Input file after execution

$ cat input.file
Value1
Value2
server1/mylogin,mypasswd [NO CHANGE, But I want this Changed]
Value3
Value4

Please help!!

---------- Post updated 05-14-10 at 12:03 AM ---------- Previous update was 05-13-10 at 11:49 PM ----------

Got it to work by using the following code:

perl -p -i -e "s!$line!$mask_text!g" input.file

Not sure if there is any other effective way!

Anyway thanks all for your inputs! its always learning with UNIX.COM :):b:

Note that the -i option for sed is specific to GNU sed and may not be available on non-GNU/Linux platforms.

# 3) logon_var=$line
3) logon_var=${line/server1\/mylogin,mypasswd/SERVER\/USERNAME,PASSWORD}