LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Replacing using sed with variables from another file? (https://www.linuxquestions.org/questions/programming-9/replacing-using-sed-with-variables-from-another-file-503885/)

eamesj 11-22-2006 07:27 AM

Replacing using sed with variables from another file?
 
Hi,,

I'm using a fortran script to output a file with a set of variables. I then need to replace a series of numbers (11111, 22222...) within a larger job with those created in the fortran.

Originally I was using 'set' to define them eg:

set line = '1600P2'
set seq = '087'

cat ${job} |\
sed -e s~11111~$line~g |\
sed -e s~22222~$seq~g >! ${outfile}

which would do the trick but involves entering the file and changing line, seq, the .f90 code reads from command line and creates a collumated file (parametersfile) with these variables.

I would like to be able to use awk to extract the correct collumn in this file and replace the variables in the main job

for example:

cat ${job} |\
./.params |\
sed -e s~11111~|awk '{print $1}' parametersfile|~g |\
sed -e s~22222~|awk '{print $1}' parametersfile|~g >! ${outfile}

Obviously this doesnt work so is there a way of doing this by changing the set preamble, changing the sed string or perhaps a different command altogether?

Many thanks,
Jon.

eamesj 11-23-2006 05:56 AM

Hi again,

It may be simpler if I refine my query… is it possible to create a temporary parameter that links to the value in another file?

for example:
set temp = /usr/dir/proj/filename
or
temp = awk ‘{print $1}’ filename

so that:
cat ${job} |\
sed -e s~11111~$temp~g > ${outfile}


Otherwise is there a way of piping awk through sed to replace 11111 with the variable?

J.

chrism01 11-24-2006 12:02 AM

This seems to do what you want

Code:

file t.t:
tull

code:
t=tttt
export t
sed -i -e  "s/tull/${t}/" t.t

file now:
tttt


eamesj 11-24-2006 03:17 AM

Quote:

file t.t:
tull

code:
t=tttt
export t
sed -i -e "s/tull/${t}/" t.t

file now:
tttt

Hi Chris,

That'll work but you still have to define t within the code, which is a pain if you've lots of variables changing in the file.
I would like the script to find the value within the file so that when the file changes, I dont need to re-enter the script.

Jon.

eamesj 11-24-2006 09:42 AM

Nearly there, I think. Re: Replacing using sed with variables from another file?
 
Hi folks,

just had a mini breakthrough

if I:
Code:

set infile1 = file

foreach item (`cat $infile1 | awk '{print $1}'`)
    cat ${job} |\
        sed -e s~11111~$item~g > ${outfile}

it'll change the job file
but

I cant as yet run many foreach strings like:

Code:


foreach item (`cat $infile1 | awk '{print $1}'`)
    cat ${job} |\
        sed -e s~11111~$item~g > ${outfile}

foreach item (`cat $infile1 | awk '{print $2}'`)
    cat ${job} |\
        sed -e s~11111~$item~g >> ${outfile}

Ideas anyone??:confused:

Jon.

eamesj 11-24-2006 10:20 AM

Hi again folks,

managed to get it working by:

Code:


set infile1 = <file>

foreach item1 (`cat $infile1 | awk '{print $1}'`)
    set item2= `grep $item $infile1 | awk '{print $2}'`
    set item3= `grep $item $infile1 | awk '{print $3}'`
    ...
        cat ${job} |\
            sed -e s~11111~$item1~g |\
            sed -e s~11111~$item2~g |\
            sed -e s~22222~$item3~g > ${outfile}

not particularly pretty or clever, but whatever gets the job done right?:p

Jon


All times are GMT -5. The time now is 12:06 AM.