Computing.Net > Forums > Unix > how to replace a line in a file

how to replace a line in a file

Reply to Message Icon

Original Message
Name: arun.s
Date: November 15, 2005 at 03:00:26 Pacific
Subject: how to replace a line in a file
OS: unix
CPU/Ram: 255
Comment:

my problem is i want to replace a line in a file

e.g
i want to replace the line
DBUID= (some name)
with
DBUID=$user variable

file name is sample.ksh
DBUID=aruns010

please help me in solving this problem. i want to replace the entire line by using DBUID as the search parameter.i dont know the characters after DBUID=....?

help me out as soon as possible

thanks



Report Offensive Message For Removal

Response Number 1
Name: nails
Date: November 15, 2005 at 13:12:00 Pacific
Subject: how to replace a line in a file
Reply: (edit)

It's kludgy, but if the line begins with DBUID, this [n]awk script replaces the entire line with variable $DBUID:

#!/bin/ksh

user=dave
DBUID="DBUID=$user"

# Solaris uses nawk, you might need awk
nawk ' {
if(/^DBUID/)
$0=v1

print
} ' v1="$DBUID" sample.ksh


Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: November 15, 2005 at 15:48:14 Pacific
Subject: how to replace a line in a file
Reply: (edit)

Or how about

sed "s/\(^DBUID=\)\(..*$\)/\1${yourvar}" test.dat


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: November 15, 2005 at 22:15:04 Pacific
Subject: how to replace a line in a file
Reply: (edit)

William:

I tried running your sed script above and I'm getting a garbled command error. I tried it on Solaris 7 and Red Hat Linux 7.1

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 4
Name: arun.s
Date: November 16, 2005 at 00:05:24 Pacific
Subject: how to replace a line in a file
Reply: (edit)

hi Nails:

give me some other solution man.. its too urgent

thanks
Arun


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: November 16, 2005 at 07:10:25 Pacific
Subject: how to replace a line in a file
Reply: (edit)

Arun:

I don't have another solution. What's wrong with the awk solution I gave you?


Report Offensive Follow Up For Removal


Response Number 6
Name: arun.s
Date: November 16, 2005 at 20:20:41 Pacific
Subject: how to replace a line in a file
Reply: (edit)

hi nails

its ok man.. i got the solution for the problem. thanks for the help..

thanks
Arun


Report Offensive Follow Up For Removal

Response Number 7
Name: WilliamRobertson
Date: November 18, 2005 at 16:34:32 Pacific
Subject: how to replace a line in a file
Reply: (edit)

Apologies, lost a trailing slash somehow. This works for me:

sed "s/\(^DBUID=\)\(..*$\)/\1${yourvar}/" test.dat


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: how to replace a line in a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software