LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-22-2006, 07:27 AM   #1
eamesj
Member
 
Registered: May 2006
Posts: 51

Rep: Reputation: 1
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.
 
Old 11-23-2006, 05:56 AM   #2
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
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.
 
Old 11-24-2006, 12:02 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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
 
Old 11-24-2006, 03:17 AM   #4
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
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.
 
Old 11-24-2006, 09:42 AM   #5
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
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??

Jon.
 
Old 11-24-2006, 10:20 AM   #6
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
Talking

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?

Jon
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 10:21 AM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM
replacing pattern with sed produces double realos Programming 1 10-17-2002 08:03 PM


All times are GMT -5. The time now is 02:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration