Why does a sed command work interactively but not in my script? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
This question already has an answer here:
How can I use variables when doing a sed?
11 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
Difference between ' and " on command line (bash)?
2 answers
SED command not replacing in bash under Debian, but works in command line.
Works in command line:
sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml
Not working in bash script, does not replace the string:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
echo "sed -i 's|$path_from|$path_to|g' $file"
sed -i 's|$path_from|$path_to|g' $file
Why doesn't replace string in file when using sed in bash?
bash shell-script command-line sed
marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
1
down vote
favorite
This question already has an answer here:
How can I use variables when doing a sed?
11 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
Difference between ' and " on command line (bash)?
2 answers
SED command not replacing in bash under Debian, but works in command line.
Works in command line:
sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml
Not working in bash script, does not replace the string:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
echo "sed -i 's|$path_from|$path_to|g' $file"
sed -i 's|$path_from|$path_to|g' $file
Why doesn't replace string in file when using sed in bash?
bash shell-script command-line sed
marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
How can I use variables when doing a sed?
11 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
Difference between ' and " on command line (bash)?
2 answers
SED command not replacing in bash under Debian, but works in command line.
Works in command line:
sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml
Not working in bash script, does not replace the string:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
echo "sed -i 's|$path_from|$path_to|g' $file"
sed -i 's|$path_from|$path_to|g' $file
Why doesn't replace string in file when using sed in bash?
bash shell-script command-line sed
This question already has an answer here:
How can I use variables when doing a sed?
11 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
Difference between ' and " on command line (bash)?
2 answers
SED command not replacing in bash under Debian, but works in command line.
Works in command line:
sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml
Not working in bash script, does not replace the string:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
echo "sed -i 's|$path_from|$path_to|g' $file"
sed -i 's|$path_from|$path_to|g' $file
Why doesn't replace string in file when using sed in bash?
This question already has an answer here:
How can I use variables when doing a sed?
11 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
Difference between ' and " on command line (bash)?
2 answers
bash shell-script command-line sed
edited Jan 22 at 13:05
JdeBP
28.6k459134
28.6k459134
asked Jan 22 at 6:57
klor
142112
142112
marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)
sed -i "s|$path_from|$path_to|g" "$file"
should work better. (I've added double quotes around $file
just in case your filename ever contains spaces.)
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
add a comment |Â
up vote
1
down vote
As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.
It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.
You could try to backup file before changing:
cp /path/to/file,.backup
sed -i 'place your pattern here' /path/to/file
and you will get file with name file.backup
.
You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.
The second safety method:
mv /path/to/file,.backup;
cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file
The next point. As bashFAQ thinks:
Embedding shell variables in sed commands is never a good idea
Thats why you need to use awk
with -v
options instead. Your script may look like this:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
sed -i 's|$path_from|$path_to|g' $file
mv $file,.backup
cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
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
accepted
Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)
sed -i "s|$path_from|$path_to|g" "$file"
should work better. (I've added double quotes around $file
just in case your filename ever contains spaces.)
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
add a comment |Â
up vote
2
down vote
accepted
Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)
sed -i "s|$path_from|$path_to|g" "$file"
should work better. (I've added double quotes around $file
just in case your filename ever contains spaces.)
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)
sed -i "s|$path_from|$path_to|g" "$file"
should work better. (I've added double quotes around $file
just in case your filename ever contains spaces.)
Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)
sed -i "s|$path_from|$path_to|g" "$file"
should work better. (I've added double quotes around $file
just in case your filename ever contains spaces.)
answered Jan 22 at 7:04
Ulrich Schwarz
8,88512643
8,88512643
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
add a comment |Â
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
â klor
Jan 22 at 7:08
add a comment |Â
up vote
1
down vote
As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.
It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.
You could try to backup file before changing:
cp /path/to/file,.backup
sed -i 'place your pattern here' /path/to/file
and you will get file with name file.backup
.
You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.
The second safety method:
mv /path/to/file,.backup;
cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file
The next point. As bashFAQ thinks:
Embedding shell variables in sed commands is never a good idea
Thats why you need to use awk
with -v
options instead. Your script may look like this:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
sed -i 's|$path_from|$path_to|g' $file
mv $file,.backup
cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
add a comment |Â
up vote
1
down vote
As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.
It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.
You could try to backup file before changing:
cp /path/to/file,.backup
sed -i 'place your pattern here' /path/to/file
and you will get file with name file.backup
.
You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.
The second safety method:
mv /path/to/file,.backup;
cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file
The next point. As bashFAQ thinks:
Embedding shell variables in sed commands is never a good idea
Thats why you need to use awk
with -v
options instead. Your script may look like this:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
sed -i 's|$path_from|$path_to|g' $file
mv $file,.backup
cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
add a comment |Â
up vote
1
down vote
up vote
1
down vote
As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.
It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.
You could try to backup file before changing:
cp /path/to/file,.backup
sed -i 'place your pattern here' /path/to/file
and you will get file with name file.backup
.
You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.
The second safety method:
mv /path/to/file,.backup;
cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file
The next point. As bashFAQ thinks:
Embedding shell variables in sed commands is never a good idea
Thats why you need to use awk
with -v
options instead. Your script may look like this:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
sed -i 's|$path_from|$path_to|g' $file
mv $file,.backup
cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file
As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.
It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.
You could try to backup file before changing:
cp /path/to/file,.backup
sed -i 'place your pattern here' /path/to/file
and you will get file with name file.backup
.
You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.
The second safety method:
mv /path/to/file,.backup;
cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file
The next point. As bashFAQ thinks:
Embedding shell variables in sed commands is never a good idea
Thats why you need to use awk
with -v
options instead. Your script may look like this:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
sed -i 's|$path_from|$path_to|g' $file
mv $file,.backup
cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file
answered Jan 22 at 10:04
Egor Vasilyev
1,792129
1,792129
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
add a comment |Â
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
â klor
Jan 22 at 11:00
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
@klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
â Egor Vasilyev
Jan 22 at 11:06
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
â klor
Jan 22 at 11:10
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
â klor
Jan 22 at 11:17
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
filenames can contain new line symbols or something else. This symbols must be handled safely.
â Egor Vasilyev
Jan 22 at 11:21
add a comment |Â