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!
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.
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:
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.
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!
Hi All,
I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination.
# file_copy.sh
source_dir = ${source_dir}
target = ${target_dir}
cp source_dir target
and my parameter file is like below
#parameter_file.txt
source_dir =... (4 Replies)
Hi
Im trying to run zip shell command from an Oracle job, but this has limitations.
This should take a few of explanaition,..
Oracle allows me to execute a command and then I can set up a fixed number of arguments. Ex: (summarizing in something like..):
JOB DEFINITION
job_name: test... (4 Replies)
I have an for loop that reads the following file
cat param.cfg
val1:env1:opt1
val2:env2:opt2
val3:env3:opt3
val4:env4:opt4
.
.
The for loop extracts the each line of the file so that at any one point, the value of i is
val1:env1:opt1 etc...
I would like to extract each... (19 Replies)
Hi ..
Can any one please tell how to pass argument to shell script at runtime?
I want to implement funcnality just like bc, where we can provide input while script is running and can be used later in the same script.
Thanks in advance... (1 Reply)
I need to pass arguments to a shell script.My batch is calling some java program.
#################
x=$1
y=$2
java -classpath program
###################
if first parameter and second parameter is null then
java -classpath program
if first parameter is not null and second parameter is... (3 Replies)
Hi
I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like
./abc def /file <directory location>
In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Hi,
I have a script which adds the user credentials to an ldap server. Im passing the variables as below..
/path/my_script $uname $pwd $environ ${deposit}
If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Hi,
I have a shell script Scp_1.sh for which I have to pass 2 arguments to run.
I have another script Scp_2.sh which in turns calls script Scp_1.sh inside.
How do I make Scp_1.sh script to read arguments automatically from a file, while running Scp_2.sh?
--
Weblogic Support (4 Replies)
Is it possible to pass a string as an argument from the command line?
I know I can pass a word in but can I put a line of text in with spaces and fullstops or do I just put it in brackets or quotes so the compiler can differinate between the first argument and the second. (1 Reply)
I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts:
#!/bin/ksh
read fname
#if (( $# > 0 )); then
$fname | ls -l
#fi
this produces a long listing of all the files in my current... (4 Replies)