使用sed删除变量

时间:2022-12-27 15:45:55

I am trying to use sed to delete a line which contains the variable $filegrep:

我试图使用sed删除包含变量$ filegrep的行:

filename=$(basename "$1")
filegrep=$(grep "$filename" /home/$USER/Desktop/Trash/Index/Index.txt)
filefir=$(dirname "filegrep")

I am attempting to do it with this command:

我试图用这个命令来做:

sed -i '/$filegrep/d' /home/$USER/Desktop/Trash/Index/Index.txt

However, I receive the error

但是,我收到错误

sed: no input files

1 个解决方案

#1


1  

Strings in '-quoted vars don't expand variables:

'-quoted vars中的字符串不会扩展变量:

marc@panic:~$ touch test.txt
marc@panic:~$ X=test.txt
marc@panic:~$ ls '$X'    <---single-quoted
ls: cannot access $X: No such file or directory
marc@panic:~$ ls "$X"    <---double-quoted
test.txt

Change your quotes in the sed call to ":

将sed调用中的引号更改为“:

sed -i "/$filegrep/d" /home/$USER/Desktop/Trash/Index/Index.txt
       ^--          ^--

#1


1  

Strings in '-quoted vars don't expand variables:

'-quoted vars中的字符串不会扩展变量:

marc@panic:~$ touch test.txt
marc@panic:~$ X=test.txt
marc@panic:~$ ls '$X'    <---single-quoted
ls: cannot access $X: No such file or directory
marc@panic:~$ ls "$X"    <---double-quoted
test.txt

Change your quotes in the sed call to ":

将sed调用中的引号更改为“:

sed -i "/$filegrep/d" /home/$USER/Desktop/Trash/Index/Index.txt
       ^--          ^--