ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am having a little difficulty with sed, and am sure this is something simple- none the less...
I have a text file that I want to search replace: Template.txt
I have another text file that will supply the replace terms: Data.txt
My script, alter.sh is as follows:
...
sed -e "s/flag-1/$1" < Template.txt > $1.txt
...
This works if I enter the command :
alter.sh newflag
BUT if I use :
alter.sh < Data.txt
nothing happens. This was verified with a quick addition of echo on the line. the variable $1 is not defined when I use the file redirection. What am I doing wrong?
As always, thanks for any assistance.
In your first example, you are passing an argument to the script. By convention, the first argument appears as $1, the second as $2, etc.
To use redirection, the script has to be reading---ie it has to be looking for input. The redirection simply says: "get it here instead of from stdin."
I only want one variable read, it is reused a few times in different places. Is what I have still appropriate?
Quote:
To use redirection, the script has to be reading---ie it has to be looking for input. The redirection simply says: "get it here instead of from stdin."
So sed is looking in Data.txt for the character strings instead of the input line (which points to Template.txt)? As I was trying to write the question up for this post I was wondering if that is what was happening. Excuse my ignorance here, but how would I write it so the script is looking for the input when the script is called? I know I could call the script and it can ask for the input file, but I would rather have the functionality resemble the way I call it in my previous post. I know this is pretty basic, but I just don't remember .
Pixellany, Thanks for the continued support!
I have actually tried your approach before posting the question, but have ended with no joy. I can get the cli command to work as intended, without the variable input in the command. I moved on to the shell script which also works, with the variable input, but not as I suspect:
This is the script:
[D@localhost Documents]$ ./custom.sh test
sed -e s/input.dat/test/<Template.R>test.R
[D@localhost Documents]$ ./custom.sh < data.lst
sed -e s/input.dat//<Template.R>.R
[D@localhost Documents]$
So when I put a single variable in my call to the script, it works (Oh, I forgot to say that I put in an echo at the start of the line for de-bugging). When I use the redirect, I get an empty variable. I ashamed to say I have spent hours of this trying to get it to work. I tried to distil my issues down to a single problem, and I think it is my understanding of the file redirection, but I could be wrong.
You can't use redirection to set the value of a variable (at least not directly).
In your example above, "custom.sh" is not expecting any input, so the redirection does nothing.
I assume that you are aware that the outer quotes is telling the echo command to output the literal string---If you wanted it to be the output of the sed command, it would be:
echo $(<command stuff>)
OK. Now I am getting somewhere. I understood that the echo was only returning the string, not the command, what you wrote would have been a little more helpful, but I don't think I knew it. I do now though!
So I can't use file redirection to set the variable value, can you suggest an efficient way to do this. The first thing that comes to my mind is to ask for input on the command line, but that seems hokey. I think I tried
Quote:
cat data.lst | custom.sh
But I don't think I got anything different from that either.
"cat" with a pipe does exactly the same thing as redirection. It does not work because the script is not expecting an input!!
Back to basics:
1. You can pass variables (arguements) when calling a command (script). Inside the script they are named $1, $2, etc.
2. You can have the script read from a file:
A. Specified internally
B. Specified externally---using redirection or the pipe method you just showed.
Regardless of how you do it, the syntax in the script needs to get the data out of the file and into wherever you need it.
Go back to my earlier post and try the examples.....
Try this:
Code:
while read -p "enter a word: " item; do echo "enter a word"; echo $item; done <filename
Hahaha! Thanks for your patience- I actually tried your example the way I though it should (before I posted), and (as you can imagine) it didn't. That's why I though it was my understanding of the redirection. I get what you are saying now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.