passing in arguments into a file using a loop

Hi, so Im a bit new to shell scripting and want to do the following but not sure how. Basically I have a file named "output" which contains misc text but inside the file I want to set up variables like $1 or some symbol. Anyways, in another file called "list" I have a list of items that I want to substitute for those variables in "output" The "list" file will have each element on each line. Anyways I figured Id use a for loop to do this but I'm not sure how to prepare my "output" file to take in variables and the number of elements in the "list" file will vary so I can exactly hard code the number. Anyone have any ideas how to accomplish this? Thanks!

IT's not clear exactly what you are trying to do, but to loop through a file, you use:

while IFS= read -r line
do
   : do something with "$line" here
done < FILENAME

Or use awk.

hey, thanks for the response and sorry for the confusion. But basically I'll give an example of what I wanted to do. I have 2 files that look like...

File 1:
Item1
Item2
Item3

File 2:
<a href= ...>
$1
<more html stuff>
$2

So basically the $1 and $2 I want them to be replaced by Item1 and Item2 and so on. Not sure if this is possible or not. I'm not exactly sure how to pass in arguments like that into a file but Im starting to understand the while loop in shell now

It would be a lot more efficient to run a single sed script over the whole file.

You can create a sed script from your file 1 -- curiously, also using sed. Feel your mind wrinkle a bit if you haven't done this before.

nl file1 |
sed 's%^[ 	 ]*\([1-9][0-9]*\)[ 	 ]*\([^ 	].*\)%s/\\$\1/\2/g%'

This adds line numbers (with nl) then uses sed to break it up. We want the line number in \1 and the original input line in \2, trimming the whitespace in between. So we are looking for beginning of line ^ followed by optional whitespace (character class with space and tab in it, zero or more occurrences) followed by a non-zero number and any number of trailing numbers, which we grab into \1; followed by more whitespace, and finally grab the rest starting from the first non-whitespace character until the end of line into \2.

To type [(space)(tab)] or [^(space)(tab)] use an editor, or, at the command line, you may need to prefix the tab with a ctrl-v in order to actually type it (because in many shells, the tab key is normally used for file name expansion).

The output is a sed script which you can save to a file, or pipe to another sed instance:

s/\$1/Item1/g
s/\$2/Item2/g
s/\$3/Item3/g

To pull it all together:

nl file1 |
sed 's%^[ 	 ]*\([1-9][0-9]*\)[ 	 ]*\([^ 	].*\)%s/\\$\1/\2/g%' | 
sed -f - file2

Thanks for the reply but it doesn't look like sed would work. The lines won't correspond like 1 to 2 and 2 to file from file1 to file2 respectively. Basically file 2 is an html page variables located throughout and I want to just pass in values to those variables and output that final html page.

Did you try? From your description, I think it still sounds like it might work, but if not, there's probably something more to learn about your specific problem. If your variable names are not $1 $2 etc then you will need to clarify what they are.

Here's a quick experiment / demonstration. file1 contains variable values, line number indicates which is which; file2 contains HTML with $1 $2 etc.

Because the stuff in file1 contains slashes, I swapped the % and / separators in the sed script.

vnix$ cat >file1
<a href="http://unix.com/">
<blink>horrible</blink>
<p>The whole paragraph is here.</p>
</a> <!-- oops, forgot the close tag for $1 (-: -->
^D
vnix$ cat >file2
<html><head><title>welcome to $2</title></head>
<body>
<p>Please visit $1the best web site evah$4
$3
</body></html>
^D
vnix$ nl file1 |
> sed 's/^[ 	 ]*\([1-9][0-9]*\)[ 	 ]*\([^ 	].*\)/s%\\$\1%\2%g/' |
> sed -f - file2
<html><head><title>welcome to <blink>horrible</blink></title></head>
<body>
<p>Please visit <a href="http://unix.com/">the best web site evah</a> <!-- oops, forgot the close tag for $1 (-: -->
<p>The whole paragraph is here.</p>
</body></html>

Hi, yea I gave it a try and it gave me some error with sed. Anyways, I guess the easiest way to describe this is give an example and the output I'm trying to create. The file1 doesn't have any variables in it. This is something I'm looking to achieve:

File1:
abc
blah
test
moretest

File2:
<html><head><title>welcome to $1</title></head>
<body>
<p>Please visit $2the best web site evah$4
$3
</body></html>

Output:
<html><head><title>welcome to abc</title></head>
<body>
<p>Please visit blahthe best web site evahmoretest
test
</body></html>

So basically stuff from file1 replaces those $# in file2. When I first ran the sed command, I ran it in a script.sh which I'm not sure makes a difference or not. Let me know what you think, thanks!

This is what happened when I tried running the command:

unix$ nl file1 |
> sed 's/^[ ]*\([1-9][0-9]\)[ ]\([^ ].*\)/s%\\$\1%\2%g/' |
> sed -f - file2
sed: -: No such file or directory

Really, your sed doesn't accept a script on standard input? Then you'll need to save it to a temporary file, and use that.

blah blah >/tmp/fix.sed
sed -f /tmp/fix.sed file2

cool, got it! One last thing and this might not even matter since it's HTML but I guess it depends on what kind of space this is but the output seemed to have added a tab right before replacing a variable so my output looks like:

<html><head><title>welcome to [tab here]abc</title></head>
<body>
<p>Please visit [tab here]blahthe best web site evah [tab here]moretest
[tab here]test
</body></html>

Any way to remove that?

sorry tab wasn't showing up in post

Dunno where that would come from, did you copy+paste the code correctly?

(Maybe I put in a tab too many somewhere, too. If you don't have tabs in your input then just use plain ole spaces throughout.)

not a problem, i can figure that out. thanks so much for the help! you rock!