How can I use a shell variable to copy an sed pattern?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have SED patterns:
[^a-zA-Z0-9]
/b./s/.*c.. ([^ ]*) .*/1/p
and so on.
I need to pass these to an echo command as variables.
At the moment, I define the $pattern variable like so:
$pattern="[^a-zA-Z0-9]"
and then pipe it to echo, like so:
echo "$OUTPUT" | sed "s/$pattern/g"
But the code is not passing the pattern, but a command and returns the error
=[^a-zA-Z0-9]: command not found
What's going wrong?
bash sed regular-expression
add a comment |Â
up vote
0
down vote
favorite
I have SED patterns:
[^a-zA-Z0-9]
/b./s/.*c.. ([^ ]*) .*/1/p
and so on.
I need to pass these to an echo command as variables.
At the moment, I define the $pattern variable like so:
$pattern="[^a-zA-Z0-9]"
and then pipe it to echo, like so:
echo "$OUTPUT" | sed "s/$pattern/g"
But the code is not passing the pattern, but a command and returns the error
=[^a-zA-Z0-9]: command not found
What's going wrong?
bash sed regular-expression
Doespattern='[^a-zA-Z0-9]'work better? You're not assigning a variable with$pattern=. Take a look atevalas an alternative too... I can make it into a proper answer if it works...
â Zip
Dec 7 '17 at 16:55
see also: unix.stackexchange.com/q/32907/117549
â Jeff Schaller
Dec 7 '17 at 17:00
[^a-zA-Z0-9]is a pattern but/b./s/.c.. ([^ ]) .*/1/pis a fullsedscript.
â Stéphane Chazelas
Dec 7 '17 at 17:18
1
Just a little insight on the error. With dollar signbashexpands it as if it were a variable and because there was nothing in$patternat that point,bashsaw this:=[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
â PesaThe
Dec 7 '17 at 17:57
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have SED patterns:
[^a-zA-Z0-9]
/b./s/.*c.. ([^ ]*) .*/1/p
and so on.
I need to pass these to an echo command as variables.
At the moment, I define the $pattern variable like so:
$pattern="[^a-zA-Z0-9]"
and then pipe it to echo, like so:
echo "$OUTPUT" | sed "s/$pattern/g"
But the code is not passing the pattern, but a command and returns the error
=[^a-zA-Z0-9]: command not found
What's going wrong?
bash sed regular-expression
I have SED patterns:
[^a-zA-Z0-9]
/b./s/.*c.. ([^ ]*) .*/1/p
and so on.
I need to pass these to an echo command as variables.
At the moment, I define the $pattern variable like so:
$pattern="[^a-zA-Z0-9]"
and then pipe it to echo, like so:
echo "$OUTPUT" | sed "s/$pattern/g"
But the code is not passing the pattern, but a command and returns the error
=[^a-zA-Z0-9]: command not found
What's going wrong?
bash sed regular-expression
edited Dec 7 '17 at 17:27
ilkkachu
50.1k676138
50.1k676138
asked Dec 7 '17 at 16:45
Michael Riordan
385
385
Doespattern='[^a-zA-Z0-9]'work better? You're not assigning a variable with$pattern=. Take a look atevalas an alternative too... I can make it into a proper answer if it works...
â Zip
Dec 7 '17 at 16:55
see also: unix.stackexchange.com/q/32907/117549
â Jeff Schaller
Dec 7 '17 at 17:00
[^a-zA-Z0-9]is a pattern but/b./s/.c.. ([^ ]) .*/1/pis a fullsedscript.
â Stéphane Chazelas
Dec 7 '17 at 17:18
1
Just a little insight on the error. With dollar signbashexpands it as if it were a variable and because there was nothing in$patternat that point,bashsaw this:=[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
â PesaThe
Dec 7 '17 at 17:57
add a comment |Â
Doespattern='[^a-zA-Z0-9]'work better? You're not assigning a variable with$pattern=. Take a look atevalas an alternative too... I can make it into a proper answer if it works...
â Zip
Dec 7 '17 at 16:55
see also: unix.stackexchange.com/q/32907/117549
â Jeff Schaller
Dec 7 '17 at 17:00
[^a-zA-Z0-9]is a pattern but/b./s/.c.. ([^ ]) .*/1/pis a fullsedscript.
â Stéphane Chazelas
Dec 7 '17 at 17:18
1
Just a little insight on the error. With dollar signbashexpands it as if it were a variable and because there was nothing in$patternat that point,bashsaw this:=[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
â PesaThe
Dec 7 '17 at 17:57
Does
pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...â Zip
Dec 7 '17 at 16:55
Does
pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...â Zip
Dec 7 '17 at 16:55
see also: unix.stackexchange.com/q/32907/117549
â Jeff Schaller
Dec 7 '17 at 17:00
see also: unix.stackexchange.com/q/32907/117549
â Jeff Schaller
Dec 7 '17 at 17:00
[^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.â Stéphane Chazelas
Dec 7 '17 at 17:18
[^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.â Stéphane Chazelas
Dec 7 '17 at 17:18
1
1
Just a little insight on the error. With dollar sign
bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.â PesaThe
Dec 7 '17 at 17:57
Just a little insight on the error. With dollar sign
bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.â PesaThe
Dec 7 '17 at 17:57
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
123ABC
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"
123XYZABC
And...
Shell variables are assigned without a leading
$.
add a comment |Â
up vote
1
down vote
Replace
$pattern="[^a-zA-Z0-9]"
by
pattern="[^a-zA-Z0-9]"
Shell variables are assigned without a leading $.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
123ABC
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"
123XYZABC
And...
Shell variables are assigned without a leading
$.
add a comment |Â
up vote
2
down vote
$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
123ABC
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"
123XYZABC
And...
Shell variables are assigned without a leading
$.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
123ABC
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"
123XYZABC
And...
Shell variables are assigned without a leading
$.
$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
123ABC
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"
123XYZABC
And...
Shell variables are assigned without a leading
$.
answered Dec 7 '17 at 16:57
FaxMax
428219
428219
add a comment |Â
add a comment |Â
up vote
1
down vote
Replace
$pattern="[^a-zA-Z0-9]"
by
pattern="[^a-zA-Z0-9]"
Shell variables are assigned without a leading $.
add a comment |Â
up vote
1
down vote
Replace
$pattern="[^a-zA-Z0-9]"
by
pattern="[^a-zA-Z0-9]"
Shell variables are assigned without a leading $.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Replace
$pattern="[^a-zA-Z0-9]"
by
pattern="[^a-zA-Z0-9]"
Shell variables are assigned without a leading $.
Replace
$pattern="[^a-zA-Z0-9]"
by
pattern="[^a-zA-Z0-9]"
Shell variables are assigned without a leading $.
answered Dec 7 '17 at 16:55
Patrick Mevzek
2,0381721
2,0381721
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409521%2fhow-can-i-use-a-shell-variable-to-copy-an-sed-pattern%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Does
pattern='[^a-zA-Z0-9]'work better? You're not assigning a variable with$pattern=. Take a look atevalas an alternative too... I can make it into a proper answer if it works...â Zip
Dec 7 '17 at 16:55
see also: unix.stackexchange.com/q/32907/117549
â Jeff Schaller
Dec 7 '17 at 17:00
[^a-zA-Z0-9]is a pattern but/b./s/.c.. ([^ ]) .*/1/pis a fullsedscript.â Stéphane Chazelas
Dec 7 '17 at 17:18
1
Just a little insight on the error. With dollar sign
bashexpands it as if it were a variable and because there was nothing in$patternat that point,bashsaw this:=[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.â PesaThe
Dec 7 '17 at 17:57