Why don my variables like $var get expanded in my sed script?
Because your sed script uses ‘single quotes’ instead of “double quotes”. Unix shells never expand $variables in single quotes. This is probably the most frequently-asked sed question. For more info on using variables, see section 4.8. 5.2. I’m using ‘p’ to print, but I have duplicate lines sometimes. Sed prints the entire file by default, so the ‘p’ command might cause the duplicate lines. If you want the whole file printed, try removing the ‘p’ from commands like ‘s/foo/bar/p’. If you want part of the file printed, run your sed script with -n flag to suppress normal output, and rewrite the script to get all output from the ‘p’ comand. If you’re still getting duplicate lines, you are probably finding several matches for the same line. Suppose you want to print lines with the words “Peter” or “James” or “John”, but not the same line twice. The following command will fail: sed -n ‘/Peter/p; /James/p; /John/p’ files Since all 3 commands of the script are executed for each line, you’ll get
Related Questions
- My firewall starts and restarts fine but if I try shorewall restore, the script fails because none of my shell variables from /etc/shorewall/params are set. Why?
- Do I need to set any environment variables in the agents script or file in order to manage objects?
- Why don my variables like $var get expanded in my sed script?