0

I'm trying to use:

sed -n '/String1/,/String2/p' Filename

to print all lines between String1 and String2. Although I want to add String1 as a user input so,

read $userinput
sed -n '/$userinput/,/String2/p' Filename. 

But as the input is within quotation is is read as the string $userinput instead of the given input.

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
  • 4
    Closely related: [How can I use variables in the LHS and RHS of a sed substitution?](https://unix.stackexchange.com/questions/69112/how-can-i-use-variables-in-the-lhs-and-rhs-of-a-sed-substitution) – steeldriver Jul 07 '20 at 22:11

1 Answers1

0

Simply like this:

read userinput                         # without sigil '$'
sed -n "/$userinput/,/String2/p" file  # with double quotes

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82