Include strings between a pattern

Hi,

I have two files:

File1:

sample statements1

<UID = " "  PWD = " ">

sample statements2

File2:

UID ="admin"
PWD ="password"

I have to read UID and PWD from file2 and place them in file1 in between those quotes.

file1 after update:

sample statements1

<UID = "admin"  PWD = "password">

sample statements2

Thanks,
Mani

Is it just one line? Or the pattern is unique?

UID="admin"
PWD="password"
sed 's/^<UID.*PWD.*/UID = '\"$UID\"' PWD = '\"$PWD\"'>/' file
1 Like

Is this a homework assignment?

Will UID and PWD always be on the same line in File1 ?
Do you really want the spaces between the quotes in the updated File1 ?
Can UID or PWD appear more than once in File1 ?

What have you tried?

Yes the pattern is unique. I have to place the values read from file2 into file between those quotes. After update file1 should look like this

sample statements1

<UID = "admin"  PWD = "password">

sample statements2

I repeat:
Is this a homework assignment?
What have you tried?

Hi Don,

Its not what you are thinking. Am just trying it with shell. I just want to know whether it is possible in shell or not.
My approach is that i will store the UID and PWD as variables in file2. I will write a script file3.sh

file3.sh:

. file2
sed /replace the pattern from file1 with values in file 2. //use sed here.. I donno using sed command



<UID = "admin"  PWD = "password">

No need of any spaces. it should look like this.

Thanks,

In the 1st post in this thread you said File2 (note the uppercase F ) contains the code:

UID ="admin"
PWD ="password"

You haven't told us what OS or shell you're using, but there are at least two problems here:

  1. A file named File2 can't be loaded using the command . file2 . (On UNIX and Linux systems, file names are case sensitive.)
  2. The commands in File2 are not assignment statements. They invoke the utility named UID with the operand -admin and the utility named PWD with the operand =password .

If you set the contents of file2 to be valid shell variable assignments as clx suggested:

UID="admin"
PWD="password"

(Note that there are no spaces around the = in either of these commands), then the following commands in file3 should do what you want:

#!/bin/ksh
. file2
sed 's/UID.*".*".*PWD.*".*"/UID = "'"$UID"'"  PWD = "'"$PWD"'"/' file1 > file1.$$ && mv file1.$$ file1

I generally prefer using the Korn shell, but this should work with any POSIX conforming shell or any shell that accepts basic Bourne shell syntax.