具体线路在文本文件中删除 [英] Removing of specific line in text file

查看:127
本文介绍了具体线路在文本文件中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,这将能够去除如果用户键入的确切标题和作者指定的行的选项。

I am working on an option which will be able to remove the line specified if the user types in the exact title and author.

不过,我不能使它工作。

However I will not be able to make it work.

我的功能内容如下所示

fnRemoveBook()
{
echo"Title: "
read Title
echo"Author: "
read Author

if grep -Fqe "$Title:$Author" BookDB.txt; then
    sed -i '/"$Title:$Author"/ d' BookDB.txt
    echo "Book removed successfully!"
else
    echo "Error! Book does not exist!"
fi
}

该BookDB内容如下所示

The BookDB contents are shown below

Hello World:Andreas:10:5:2
Teaching:Mary Ann:50:23:5

我在Hello World的锁定式设计,标题和安地列斯作者后,程序将能够进入环和回声编书成功删除。

After I have keyed in "Hello World" for title and "Andreas" for author, the program will be able to enter into the loop and echo-ed Book removed successfully.

不过,我检查了BookDB.txt行尚未删除,我不知道为什么。

However I checked the BookDB.txt the line has not been removed yet and I wonder why.

我希望我能得到一些答案在这里,因为我已经在互联网上做研究,但仍然有目前没有答案。

I hope I will be able to get some answers out here as I have been doing research on the internet but still have no answers at the moment.

在此先感谢那些谁帮助! :)

Thanks in advance to those who helped! :)

推荐答案

您正在试图通过一个庆典变量(二,实际上)成一个子程序( SED ),但围绕你传递单引号前pression,这确实没有参数扩展。这意味着, SED 字面上看 $标题:$作者。尝试把整个 SED 前pression在双引号:

You're trying to pass a bash variable (two, actually) into a sub-program (sed), but surrounding the expression you're passing in single-quotes, which does no parameter expansion. That means that sed is literally seeing $Title:$Author. Try putting the whole sed expression in double-quotes:

sed -i "/$Title:$Author/d" BookDB.txt

这将使庆典来展开 $标题 $作者才进入 SED

This will allow bash to expand $Title and $Author before they get into sed.

这篇关于具体线路在文本文件中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆