Reputation: 75
I am using the following command to delete all text before a match in a file:
sed -n '/sweet/,$p' file
It works perfectly, but i need to change 'sweet' to a variable, as its a variable i assume i need to use double quotes instead of single quotes, however when i run this command:
sed -n "/$variable/,$p" file
i receive the following error:
sed: -e expression #1, char 11: unexpected `,'
How can i make this command work?
Upvotes: 1
Views: 3532
Reputation: 9679
Single quotes tell shell to not perform any expansion at all and sed
gets three arguments -n
, /sweet/,$p
, and file
.
When using double quotes, variables get expanded. Presuming variable=sweet
and p
not being set, second sed
call got the following three arguments: -n
, /sweet/,
, and file
. Address specifier where ,
is not followed by anything is not valid and gave you the error you've seen.
If you want to mix a (literal) $
and variable expansion in a single argument, you have few option:
"/$variable/,\$p"
which escapes the $
"/$variable/"',$p'
or similar mix of single and double quoted string parts to treat $
in each of its segment as needed.Upvotes: 4
Reputation: 674
I would brake the single quote for use a double for the variable then reopen the single quote.
sed -n '/'"${var}"'/,$p' file
I know this is working, however I not really used it yet so I don't know if this is the right way to do this.
Upvotes: 1