Since I changed the directory, I want to change all the environment variables in the file.
test.sh
../../${dir}/file1.txt
../../${dir}/file2.txt
../../${dir}/file3.txt
../../${dir}/file4.txt
../../${dir}/file5.txt
↓
test.sh
../file1.txt
../file2.txt
../file3.txt
../file4.txt
../file5.txt
I want to do this
In other words ../${dir}/ When you want to delete the part of.
Successful ones are the following commands
python
sed -i s@'../${dir}/'@@g test.sh
** Explanation **
The syntax of sed that often appears when searching is as follows (This is the reason why I have more trouble)
python
sed 's/The string you want to replace/Character string after replacement/g'Input file name
g means replace all strings in the input file.
Add options
python
sed -i 's/The string you want to replace/Character string after replacement/g'Input file name
You can overwrite files without pipe & cat by using the -i option.
I failed many times this time, so I added ".org" after the -i option.
python
sed -i".org" 's/The string you want to replace/Character string after replacement/g'Input file name
This will create test.sh.org and make a backup.
Now, it is the replacement of the special symbol of the main subject. For the time being, I tried the following.
python
sed -i".org" 's/../${dir}///g' test.sh
sed: -e expression #1, char 13: unknown option to `s'
I get an error. The slash seems to be the problem.
Then, enclose the character string you want to replace with''.
python
sed -i".org" 's/'../${dir}/'//g' test.sh
sed: -e expression #1, char 7: unknown option to `s'
If you look it up, if there is a "/ (slash)" in the string It seems good to change the sed slash to a different symbol. I used "@" this time, but ":", "*", "#", etc. It seems that any symbol that is not used in the character string will do.
python
sed -i".org" 's@'../${dir}/'@@g' test.sh
test.sh
${difile1.txt
${difile2.txt
${difile3.txt
${difile4.txt
${difile5.txt
What's this···
test.sh.org
../../${dir}/file1.txt
../../${dir}/file2.txt
../../${dir}/file3.txt
../../${dir}/file4.txt
../../${dir}/file5.txt
I'm glad I had a backup.
mv test.sh.org test.sh
I tried various things, but not all
python
sed -i".org" "s@'../${dir}/'@@g" test.sh
sed -i".org" "s@../${dir}/@@g" test.sh
After some trial and error, it seems that the slash of the sed command itself is not needed.
python
sed -i".org" s@../${dir}/@@g test.sh
Is it that the character string is properly enclosed?
python
sed -i".org" s@'../${dir}/'@@g test.sh
Succeeded.
It was a case that I thought I had to think about the meaning of quotation marks.