sed not working on a variable within a bash script; requesting a file. Simple example
up vote
1
down vote
favorite
If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.
Call my script with multiple parameters
./myScript.sh apples oranges ilike,apples,oranges,bananas
My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.
MyScript.sh
fruits="$3"
checkFruits= sed -i 's/,/ /g' <<< "$fruits"
echo $checkFruits
And the result after running the script in the terminal:
ilike,apples,oranges,bananas
sed: no input files
P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.
linux bash variables unix sed
add a comment |
up vote
1
down vote
favorite
If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.
Call my script with multiple parameters
./myScript.sh apples oranges ilike,apples,oranges,bananas
My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.
MyScript.sh
fruits="$3"
checkFruits= sed -i 's/,/ /g' <<< "$fruits"
echo $checkFruits
And the result after running the script in the terminal:
ilike,apples,oranges,bananas
sed: no input files
P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.
linux bash variables unix sed
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.
Call my script with multiple parameters
./myScript.sh apples oranges ilike,apples,oranges,bananas
My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.
MyScript.sh
fruits="$3"
checkFruits= sed -i 's/,/ /g' <<< "$fruits"
echo $checkFruits
And the result after running the script in the terminal:
ilike,apples,oranges,bananas
sed: no input files
P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.
linux bash variables unix sed
If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.
Call my script with multiple parameters
./myScript.sh apples oranges ilike,apples,oranges,bananas
My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.
MyScript.sh
fruits="$3"
checkFruits= sed -i 's/,/ /g' <<< "$fruits"
echo $checkFruits
And the result after running the script in the terminal:
ilike,apples,oranges,bananas
sed: no input files
P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.
linux bash variables unix sed
linux bash variables unix sed
edited Nov 10 at 11:20
halfer
14.2k757106
14.2k757106
asked Nov 9 at 22:05
Karim
215
215
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
The -i flag to sed requires a file argument, without it the sed command does what you expect.
However, I'd consider using tr
instead of sed
for this simple replacement:
fruits="$3"
checkFruits="$(tr , ' ' <<< $fruits)"
echo $checkFruits
Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas"
to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)
If 6, then the other answers (including mine) will already work.
However, if you want the answer to be 4, then you might want to do something else, like:
fruits="$3"
checkFruits="$(tr , \n <<< $fruits)"
itemCount="$(wc -l <<< $checkFruits)"
Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.
-i
requires an extension argument only on macOS, not on Linux
– John Weldon
Nov 9 at 22:13
On Ubuntu 18.04 using-i
gave the same error as OP described.
– Jeff Breadner
Nov 9 at 22:14
You're right - I think the OP intended to use-e
– John Weldon
Nov 9 at 22:19
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
1
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
add a comment |
up vote
3
down vote
You can do
fruits="$3"
checkFruits="${3//,/ }"
# or
echo "${3//,/ }"
1
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
1
Yeah, I totally forgot about bash's built-in string replacement. This is superior to bothsed
andtr
for the use case presented.
– Jeff Breadner
Nov 9 at 22:32
add a comment |
up vote
1
down vote
The -i
option is for inplace editing of input file, you don't need it here.
To assign a command's output to a variable, use command expansion like var=$(command)
.
fruits="$3"
checkFruits=$(sed 's/,/ /g' <<< "$fruits")
echo $checkFruits
add a comment |
up vote
0
down vote
You don't need sed
at all.
IFS=, read -a things <<< "$3"
echo "${#things[@]}"
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The -i flag to sed requires a file argument, without it the sed command does what you expect.
However, I'd consider using tr
instead of sed
for this simple replacement:
fruits="$3"
checkFruits="$(tr , ' ' <<< $fruits)"
echo $checkFruits
Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas"
to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)
If 6, then the other answers (including mine) will already work.
However, if you want the answer to be 4, then you might want to do something else, like:
fruits="$3"
checkFruits="$(tr , \n <<< $fruits)"
itemCount="$(wc -l <<< $checkFruits)"
Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.
-i
requires an extension argument only on macOS, not on Linux
– John Weldon
Nov 9 at 22:13
On Ubuntu 18.04 using-i
gave the same error as OP described.
– Jeff Breadner
Nov 9 at 22:14
You're right - I think the OP intended to use-e
– John Weldon
Nov 9 at 22:19
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
1
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
add a comment |
up vote
1
down vote
accepted
The -i flag to sed requires a file argument, without it the sed command does what you expect.
However, I'd consider using tr
instead of sed
for this simple replacement:
fruits="$3"
checkFruits="$(tr , ' ' <<< $fruits)"
echo $checkFruits
Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas"
to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)
If 6, then the other answers (including mine) will already work.
However, if you want the answer to be 4, then you might want to do something else, like:
fruits="$3"
checkFruits="$(tr , \n <<< $fruits)"
itemCount="$(wc -l <<< $checkFruits)"
Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.
-i
requires an extension argument only on macOS, not on Linux
– John Weldon
Nov 9 at 22:13
On Ubuntu 18.04 using-i
gave the same error as OP described.
– Jeff Breadner
Nov 9 at 22:14
You're right - I think the OP intended to use-e
– John Weldon
Nov 9 at 22:19
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
1
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The -i flag to sed requires a file argument, without it the sed command does what you expect.
However, I'd consider using tr
instead of sed
for this simple replacement:
fruits="$3"
checkFruits="$(tr , ' ' <<< $fruits)"
echo $checkFruits
Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas"
to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)
If 6, then the other answers (including mine) will already work.
However, if you want the answer to be 4, then you might want to do something else, like:
fruits="$3"
checkFruits="$(tr , \n <<< $fruits)"
itemCount="$(wc -l <<< $checkFruits)"
Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.
The -i flag to sed requires a file argument, without it the sed command does what you expect.
However, I'd consider using tr
instead of sed
for this simple replacement:
fruits="$3"
checkFruits="$(tr , ' ' <<< $fruits)"
echo $checkFruits
Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas"
to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)
If 6, then the other answers (including mine) will already work.
However, if you want the answer to be 4, then you might want to do something else, like:
fruits="$3"
checkFruits="$(tr , \n <<< $fruits)"
itemCount="$(wc -l <<< $checkFruits)"
Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.
edited Nov 9 at 22:29
answered Nov 9 at 22:11
Jeff Breadner
1,010617
1,010617
-i
requires an extension argument only on macOS, not on Linux
– John Weldon
Nov 9 at 22:13
On Ubuntu 18.04 using-i
gave the same error as OP described.
– Jeff Breadner
Nov 9 at 22:14
You're right - I think the OP intended to use-e
– John Weldon
Nov 9 at 22:19
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
1
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
add a comment |
-i
requires an extension argument only on macOS, not on Linux
– John Weldon
Nov 9 at 22:13
On Ubuntu 18.04 using-i
gave the same error as OP described.
– Jeff Breadner
Nov 9 at 22:14
You're right - I think the OP intended to use-e
– John Weldon
Nov 9 at 22:19
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
1
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
-i
requires an extension argument only on macOS, not on Linux– John Weldon
Nov 9 at 22:13
-i
requires an extension argument only on macOS, not on Linux– John Weldon
Nov 9 at 22:13
On Ubuntu 18.04 using
-i
gave the same error as OP described.– Jeff Breadner
Nov 9 at 22:14
On Ubuntu 18.04 using
-i
gave the same error as OP described.– Jeff Breadner
Nov 9 at 22:14
You're right - I think the OP intended to use
-e
– John Weldon
Nov 9 at 22:19
You're right - I think the OP intended to use
-e
– John Weldon
Nov 9 at 22:19
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
Exactly what I needed. Thank you! Can't upvote you unfortunately.
– Karim
Nov 10 at 12:43
1
1
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
@Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
– Walter A
Nov 10 at 19:44
add a comment |
up vote
3
down vote
You can do
fruits="$3"
checkFruits="${3//,/ }"
# or
echo "${3//,/ }"
1
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
1
Yeah, I totally forgot about bash's built-in string replacement. This is superior to bothsed
andtr
for the use case presented.
– Jeff Breadner
Nov 9 at 22:32
add a comment |
up vote
3
down vote
You can do
fruits="$3"
checkFruits="${3//,/ }"
# or
echo "${3//,/ }"
1
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
1
Yeah, I totally forgot about bash's built-in string replacement. This is superior to bothsed
andtr
for the use case presented.
– Jeff Breadner
Nov 9 at 22:32
add a comment |
up vote
3
down vote
up vote
3
down vote
You can do
fruits="$3"
checkFruits="${3//,/ }"
# or
echo "${3//,/ }"
You can do
fruits="$3"
checkFruits="${3//,/ }"
# or
echo "${3//,/ }"
answered Nov 9 at 22:30
Walter A
10.1k2930
10.1k2930
1
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
1
Yeah, I totally forgot about bash's built-in string replacement. This is superior to bothsed
andtr
for the use case presented.
– Jeff Breadner
Nov 9 at 22:32
add a comment |
1
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
1
Yeah, I totally forgot about bash's built-in string replacement. This is superior to bothsed
andtr
for the use case presented.
– Jeff Breadner
Nov 9 at 22:32
1
1
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
I like this approach better if the OP is already using bash
– John Weldon
Nov 9 at 22:32
1
1
Yeah, I totally forgot about bash's built-in string replacement. This is superior to both
sed
and tr
for the use case presented.– Jeff Breadner
Nov 9 at 22:32
Yeah, I totally forgot about bash's built-in string replacement. This is superior to both
sed
and tr
for the use case presented.– Jeff Breadner
Nov 9 at 22:32
add a comment |
up vote
1
down vote
The -i
option is for inplace editing of input file, you don't need it here.
To assign a command's output to a variable, use command expansion like var=$(command)
.
fruits="$3"
checkFruits=$(sed 's/,/ /g' <<< "$fruits")
echo $checkFruits
add a comment |
up vote
1
down vote
The -i
option is for inplace editing of input file, you don't need it here.
To assign a command's output to a variable, use command expansion like var=$(command)
.
fruits="$3"
checkFruits=$(sed 's/,/ /g' <<< "$fruits")
echo $checkFruits
add a comment |
up vote
1
down vote
up vote
1
down vote
The -i
option is for inplace editing of input file, you don't need it here.
To assign a command's output to a variable, use command expansion like var=$(command)
.
fruits="$3"
checkFruits=$(sed 's/,/ /g' <<< "$fruits")
echo $checkFruits
The -i
option is for inplace editing of input file, you don't need it here.
To assign a command's output to a variable, use command expansion like var=$(command)
.
fruits="$3"
checkFruits=$(sed 's/,/ /g' <<< "$fruits")
echo $checkFruits
edited Nov 9 at 22:30
answered Nov 9 at 22:10
oguzismail
2,269518
2,269518
add a comment |
add a comment |
up vote
0
down vote
You don't need sed
at all.
IFS=, read -a things <<< "$3"
echo "${#things[@]}"
add a comment |
up vote
0
down vote
You don't need sed
at all.
IFS=, read -a things <<< "$3"
echo "${#things[@]}"
add a comment |
up vote
0
down vote
up vote
0
down vote
You don't need sed
at all.
IFS=, read -a things <<< "$3"
echo "${#things[@]}"
You don't need sed
at all.
IFS=, read -a things <<< "$3"
echo "${#things[@]}"
answered Nov 9 at 23:04
chepner
239k29228319
239k29228319
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233844%2fsed-not-working-on-a-variable-within-a-bash-script-requesting-a-file-simple-ex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown