Operating System - HP-UX
1827462 Members
4195 Online
109965 Solutions
New Discussion

replacing a line of file with a line from another file

 
mahesh_24
Occasional Advisor

replacing a line of file with a line from another file

Can anybody let me know how to replace a line by (line to be searched by pattern ) with another line which is available in another file.
8 REPLIES 8
Mark Grant
Honored Contributor

Re: replacing a line of file with a line from another file

something like

#!/bin/sh
LINE2=$(grep "line I need" file2)
sed "s/pattern/$LINE2/" file1 > file3
Never preceed any demonstration with anything more predictive than "watch this"
mahesh_24
Occasional Advisor

Re: replacing a line of file with a line from another file

Hi Mark,
i have tried that usind sed but the error which i am getting is that pattern canot be parsed.The problem is
I have one of the user entries in passwd file ( all the 7 fields ) in one passwd file
say
oracledb:*:11:11::/var/spool/uucppublic:/usr/lbin/uucp/uucico

Now i have constructed a line

Xoracleb:*NP* :11:11:user disabled :/var/spool/uucppublic:/bin/false

using script now i wanna replace the constructed line which is there in my o/p file with the line matching "^oracledb" in the start of the line .
Note:I do not want to replace just "oracledb "

I guess sed gave me the error because the variable was long
i tried the following using sed


sed ' /'$orgvar`/ {
s/'$orgvar'/'$newvar'/
}' datafile

where $orgvar contains the old line and $newvar contains the new line which has to come in place of old line
Jean-Luc Oudart
Honored Contributor

Re: replacing a line of file with a line from another file

with special characters this might be a bit tricky

In a previous thread I mentioned the use of vi
have a look at it :
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x38fa82eced96bc409e0e67b4869c642d,00.html

Rgds,
JL
fiat lux
mahesh_24
Occasional Advisor

Re: replacing a line of file with a line from another file

Hi Jean , i read the same and have one question

is there a way to parse a shell variable in vi with can be given as $var for search patterns
Rodney Hills
Honored Contributor

Re: replacing a line of file with a line from another file

Using "awk" you could search for lines that match $orgvar and print $newvar instead-

awk -Vorg="$orgvar" -Vnew="$newvar" '{if $0 == $org then print $new else print $0}' newpasswd

Not sure your full intent here. Are you planning on changing multiple passwd files or are their multiple entries within one passwd file.

HTH

-- Rod Hills
There be dragons...
Jean-Luc Oudart
Honored Contributor

Re: replacing a line of file with a line from another file

I think you may still have the problem as long as you have special characters.
Same apply to awk.

Therefore, as mentioned in the previous thread thr "trick" is to position the cursor in the file and import an external file.

Rgds,
Jean-Luc
fiat lux
Fredrik Acosta
New Member

Re: replacing a line of file with a line from another file

Since the search pattern contains slashes '/' you can't use '/' for expression delimiter with sed - try to use another character (e.g. '@').

Example:
sed 's@find@replace@' < file1 > file2

To replace a line in one file with a line from another file - try:

(With sed)
----
#!/bin/sh

# 'file' is the file to be changed
# 'template' is a file containing strings to replace with
# 'find' is the string to search for
# 'repl' is part of the string to replace with

find=$1; template=$2; find=$3; repl=$4

replace=`grep "$repl" $template`
sed "/$find/s@@$replace@" $file > /tmp/$file.$$
mv /tmp/$file.$$ $file
----
invoke as:
script '^oracledb' '^Xoracleb'

Better is to use 'ed' (if you use the above method, you might lose permission settings on the original file depending on umask settings).

(with ed)
----
#!/bin/sh

file=$1; template=$2; find=$3; repl=$4
replace=`grep "$repl" $template`

ed - $file </$find/c
$replace
.
w
de
----

Happy scripting!
Dave La Mar
Honored Contributor

Re: replacing a line of file with a line from another file

Mahesh -
sed indeed will error out if / is in the string.
I have successfully done this by simply escaping the / with a \ in the string.
i.e. string="\/home\/someone\/or_something"

Best of luck.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."