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.
Your problem can be decomposed into two smaller problems, each with solutions.
1. You want to read stuff from a file. This reads one line from a file called sed_string, into a variable called $argVar
Code:
read argVar < sed_string
# Echo the line, just to make sure...
echo $argVar
2. You can use the variable as a commandline argument to sed. If the line read from the file might contain whitespace, you should enclose it in quotes.
Code:
sed \'${argVar}\'
You can combine these component code fragments in loops and larger code segments according to your requirements.
2. You can use the variable as a commandline argument to sed. If the line read from the file might contain whitespace, you should enclose it in quotes.
Code:
sed "${argVar}"
I agree. (theNbomr, I took the liberty to fix the quotes there).
Additionally, eddyq, if you use a Bash shell, you can compose the arguments to the command in an array first, then supply the array as the command-line parameters.
For example, you might wish to use a sed command, which takes the regular expressions from one source, edit mode options from another, and file names from third source:
Code:
#!/bin/bash
# Keep all non-ASCII characters intact, regardless of the charset.
export LANG=C LC_ALL=C
# This will contain the command line arguments for sed.
# Note that "${args[@]}" means "each separate item in args".
args=()
# Add a pattern that changes each 'foo' to 'bar'.
args=("${args[@]}" -e 's|foo|bar|g')
# Add a pattern which removes lines that begin with a # or a ;.
args=("${args[@]}" -e '/^[\t\v\f\r ]*#/ d')
# Add option -i which tells sed to edit the target files in-place,
# instead of outputting the changes to standard output.
args=("${args[@]}" -i)
# Add everything supplied on the command line to this script, too.
# (Basically, assume they're all file names or sed options.)
args=("${args[@]}" "$@")
# Execute the sed command, using parameters defined above.
sed "${args[@]}"
As you can see, this is quite easy to use, and allows you to construct very complex commands without relying on tedious if-then or case-esac structures.
Do note, however, that input and output redirections ( <input-file and >output-file ) are handled by the shell, not the command itself, so you cannot do redirections by inserting those into the array.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.