Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why don my variables like $var get expanded in my sed script?

Expanded Script SED variables
0
Posted

Why don my variables like $var get expanded in my sed script?

0

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

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123