Reputation: 373
i can't remove like one line from the text file after user inputs the book title and author(from the text file)
i tried this code
What i did:
function remove_book
{
echo "Title: "
read title
echo "Author: "
read name
echo $title $name < BookDB.txt
echo "Book $title by $name successfully removed!"
sed '/pattern to match/d' ./BookDB.txt
#echo "testing remove"
}
but then, it shows this
even it shows like this, they weren't actually removed from the file..
Title:
The Hobbit
Author:
J.R.R. Tolkien
The Hobbit J.R.R. Tolkien
Book The Hobbit by J.R.R. Tolkien successfully removed!
Harry Potter and the Deathly Hallows J.K. Rowling
The Maze Runner James Dashner
Life Without Limits Nick Vujicic
The Hobbit J.R.R. Tolkien
Desired Output:
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
Please enter your option: 2
Title : Biography of Crocodile Dundee
Author : Crox Swamplund
Error! Book does not exists!
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
Please enter your option: 2
Title : C++ for dummies
Author : Edward Scissorhands
Book Title ‘C++ for dummies’ removed successfully!
(the book and author's names in the Desired Output are just examples)
FROM
Harry Potter and the Deathly Hallows J.K. Rowling
The Maze Runner James Dashner
Life Without Limits Nick Vujicic
The Hobbit J.R.R. Tolkien [remove one line, like this Hobbit book]
TO
Harry Potter and the Deathly Hallows J.K. Rowling
The Maze Runner James Dashner
Life Without Limits Nick Vujicic
how do i remove one line? after user inputs the title and author's name? help me please thanks! :)
Upvotes: 2
Views: 1834
Reputation: 44023
Do not do this with sed. At some point, someone somewhere will write a book titled "Good times/bad times" or "Ca$h ca$h ca$h!!!1! Make $$$ in your spare time!" or "What you always wanted to know (and never dared to ask)" and sed will screw it up horribly because the special characters have meaning for its pattern matching engine.
You can do it with GNU awk like this:
awk -i inplace -v title="$title" -v author="$name" '$0 != title " " author' BookDB.txt
This will select all lines from the file that are not exactly the contents of $title
followed by one space followed by the contents of $name
. Since the shell variables are not substituted into the awk code but passed through awk's -v
parameter, no interpretation of special characters takes place.
Also: Are you sure you want to do it inplace? I like to keep a backup in case an operation goes wrong. Like
cp BookDB.txt BookDB.txt~
awk -v title="$title" -v author="$name" '$0 != title " " author' BookDB.txt~ > BookDB.txt
Then if things go wrong or you remove the wrong book, rolling back is easy. Also, this will work with other awks than the one from GNU.
Alternatively, you could use grep like this:
cp BookDB.txt BookDB.txt~
grep -vxF "$title $name" BookDB.txt~ > BookDB.txt
Where -x
tells grep to match only if the match is the whole line and -F
tells it to take the pattern as a fixed string rather than a regex.
Upvotes: 3
Reputation: 195049
you may want to add (gnu?) sed's -i
option
read man sed
to know more info about -i
expand a bit...
If you want the changed done by sed to be save in your file, you can use -i
option, edit files in place
. An example:
kent$ cat f
1
2
3
4
5
kent$ sed -i '/2/d' f
kent$ cat f
1
3
4
5
Upvotes: 1