Deleting/Removing sentence from .txt


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting/Removing sentence from .txt
# 1  
Old 01-30-2011
Deleting/Removing sentence from .txt

Hi,

now i need to remove the entires i inserted into my .txt file.

Code:
    echo -n "Title: "
    read Title
    echo -n "Author: "
    read Author
    if grep -q "$Title: $Author" "BookDB.txt"; then
	sed '$Title: $Author' BookDB.txt
	echo "Book Title '$Title' removed successfully!"		
    else
	echo "Error! Book does not exists!"
    fi

my sed command doesnt seems to work. my BookDB.txt contains the Title, Author and price of the book. Required to delete the whole sentence just by the input of Title and Author.

For E.g

ABC:John:5

where ABC = Title, John = Author, 5 = Price.

Much help would be appreciated =)

Last edited by Yogesh Sawant; 01-30-2011 at 05:30 PM.. Reason: added code tags
# 2  
Old 01-30-2011
Have a look at: grep -v
# 3  
Old 01-30-2011
Quote:
Originally Posted by Scrutinizer
Have a look at: grep -v
i need to remove the line permanently instead of prnting the non matching lines.
# 4  
Old 01-30-2011
You can direct it to a new file and then replace the original with it...
# 5  
Old 01-30-2011
meaning? can i code it in a way that it remove the sentence straight from the BookDB.txt itself instead of creating a new file to replace it?

---------- Post updated at 04:45 PM ---------- Previous update was at 04:35 PM ----------

sed "/$Title/d" BookDB.txt > BookDB1.txt

i did this for a new file. but how do i direct it back to BookDB.txt itself instead of BookDB1.txt which is a temporary file.
# 6  
Old 01-30-2011
Code:
sed "/$Title/d" BookDB.txt > BookDB1.txt && mv BookDB1.txt BookDB.txt

If you have GNU sed you can do an in place edit:
Code:
sed -i.bak "/$Title: $Author/d" BookDB.txt

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-30-2011
Thanks! PRoblems solved!

sed "/$Title/d" BookDB.txt > BookDB1.txt && mv BookDB1.txt BookDB.txt does the trick! =)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

2. Shell Programming and Scripting

Deleting Repeating lines from a txt file via script

Hi, I'm having trouble in achieving the following scenario. There is a txt file with thousands of lines and few lines are repeated, which needs to be removed using a script. File.txt 20140522121432,0,12,ram Loc=India From=ram@xxx.com, To=ravi@yyy.com,, 1 2 3 4 . . 30... (18 Replies)
Discussion started by: Gautham
18 Replies

3. Shell Programming and Scripting

Removing inline binary data from txt file

I am trying to parse a file but the filehas binary data inline mixed with text fields. I tried the binutils strings function , it get the binary data out but put the char following the binary data in a new line . input file app_id:1936 pgm_num:0 branch:TBNY ord_num:0500012(–QMK) deal_num:0... (12 Replies)
Discussion started by: tasmac
12 Replies

4. Solaris

Something is removing/deleting my wtmpx file?

hi, we have a solaris 10 box that was handled by a different sysadmin before & now it is turned over to us for system administration. our concern is that if we issue the "last" command, it usually says "wtmp begins current day current month date 02:30". just like this "wtmp begins Thu Mar 7... (6 Replies)
Discussion started by: booghaw
6 Replies

5. Shell Programming and Scripting

Deleting pattern without removing line

I am trying to delete a pattern without removing line. I searched a lot in this forum and using those I could come up with sed command but it seems that command does not work. Here's how my file looks like: 1 ./63990 7 1171 ./63990 2 2425 ./63990 9 2539 ./63990 1 3125 ./63990 1 10141... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

6. UNIX for Dummies Questions & Answers

Deleting lines in .txt with nonspecific value

Hello, i am new to the forum and know nothing about programing, Linux or Unix :( hope somebody can help me out. I have a .txt file that i need to delete certain lines from. After searching the forum i noticed that using "sed" was the way to go, so i installed gnuwin32 (i use windows xp... (4 Replies)
Discussion started by: luis3141
4 Replies

7. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

8. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies

9. Shell Programming and Scripting

Script for removing text from a txt file

Hello, So I wanted to write a very simple script to remove some information from a text file and save it as something else. For example I have a text file (let's call it txt) with three rows of numbers: 0 0 1 9 8 7 5 0 6 7 9 0 0 7 9 8 1 1 6 4 0 6 0 0 9 8 4 6 0 9 2 8 1 And I want to... (2 Replies)
Discussion started by: hertingm
2 Replies

10. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies
Login or Register to Ask a Question