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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to replace a line in a file

Reply to Message Icon

Name: arun.s
Date: November 15, 2005 at 03:00:26 Pacific
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




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 15, 2005 at 13:12:00 Pacific
Reply:

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


0

Response Number 2
Name: WilliamRobertson
Date: November 15, 2005 at 15:48:14 Pacific
Reply:

Or how about

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


0

Response Number 3
Name: nails
Date: November 15, 2005 at 22:15:04 Pacific
Reply:

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


0

Response Number 4
Name: arun.s
Date: November 16, 2005 at 00:05:24 Pacific
Reply:

hi Nails:

give me some other solution man.. its too urgent

thanks
Arun


0

Response Number 5
Name: nails
Date: November 16, 2005 at 07:10:25 Pacific
Reply:

Arun:

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


0

Related Posts

See More



Response Number 6
Name: arun.s
Date: November 16, 2005 at 20:20:41 Pacific
Reply:

hi nails

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

thanks
Arun


0

Response Number 7
Name: WilliamRobertson
Date: November 18, 2005 at 16:34:32 Pacific
Reply:

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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: how to replace a line in a file

how to replace a file in a .zip fil www.computing.net/answers/unix/how-to-replace-a-file-in-a-zip-fil/7497.html

how to replace a file and save it i www.computing.net/answers/unix/how-to-replace-a-file-and-save-it-i/7216.html

How to parse a file of 199 col? www.computing.net/answers/unix/how-to-parse-a-file-of-199-col/7250.html