Delete line in text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete line in text file
# 1  
Old 08-31-2005
Delete line in text file

Hi,
How do I go about deleting a line in a text file. I have used grep to find a particlular word, now how do I delete the line?

so far I got:

grep -i $keyword file.txt
# 2  
Old 08-31-2005
Did you use the search within this forum ?

Please do so. The rules of the forum says

(5) Search the forums database with your keywords before asking.

Vino
# 3  
Old 08-31-2005
sorry about that, wont happen again.

so if I use:

sed -e '/$keyword/d' file.txt

would that delete a user entered $keyword from a text file?
# 4  
Old 08-31-2005
When you do that, you will not see lines containing the $keyword on the terminal. But that wouldnt delete the keyword from the file. Because you are not redirecting the output to a file.

If you need the change to be permanent, then use

Code:
sed -e '/$keyword/d' file.txt >file.txt.new

If the keyword contains spaces and special characteres, you need to put the -e script within double quotes.

Code:
sed -e "/$keyword/d" file.txt >file.txt.new

Vino
# 5  
Old 08-31-2005
cheers vino, thanks for your help.
# 6  
Old 11-07-2007
I hate to bring up a dead topic but its really relavant to what I need

I need to compare 2 files and output what is the same between the 2 into a 3rd file

old.txt = abc def ghi
new.txt = abc ghi jkl
final.txt = abc ghi

Here is what I have.. Not sure why it won't work. Is it because my diff doesn't get just "jkl def"

Quote:
#!/bin/sh

keyword='diff old.txt new.txt'
sed -e '/$keyword/d' old.txt > final.txt
# 7  
Old 12-16-2008
But if i want to do this is in a loop

sed -e "/$keyword/d" file.txt >file.txt.new

the file.txt.new will have all the matching lines as in file.txt???
How will I do that??
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines above and below specific line of text

I'm trying to remove a specific number of lines, above and below a specific line of text, highlighted in red: <STMTTRN> <TRNTYPE>CREDIT <DTPOSTED>20151205000001 <TRNAMT>10 <FITID>667800001 <CHECKNUM>667800001 <MEMO>BALANCE </STMTTRN> <STMTTRN> <TRNTYPE>DEBIT <DTPOSTED>20151207000001... (8 Replies)
Discussion started by: bomsom
8 Replies

2. UNIX for Dummies Questions & Answers

Delete records based on a text file from a text file

Hi Folks, I am a novice and need to build a script in bash. I have 2 text files data.txt file is big file, column 2 is the we need to search and delete in the output. The filter file contains the rows to be deleted. Data.txt state city zone Alabama Huntsville 4 California SanDiego 3... (3 Replies)
Discussion started by: tech_frk
3 Replies

3. UNIX for Dummies Questions & Answers

How to delete text between two characters in line?

Hi, I have a large text file with the following format: >gi|347545744|gb|JN204951.1| Dismorphia spio voucher 5 ATCAAATTCCTTCCTCTCCTTAAA >gi|17544664774|gb|WN204922.32| Rodapara nigens gene region CCGGGCAAATTCCTTCCTCTCCTTAAA >gi|555466400|gb|SG255122.8| Bombyx mandariana genbank 3... (1 Reply)
Discussion started by: euspilapteryx
1 Replies

4. Shell Programming and Scripting

Find the text in the file and delete the rest of the line.

Hi, I have one requiremnet like this. I need to find some particular string (eg.IncludeDateTime = ) in a file. And wherever it finds the string the unix script has to delete the remaining text coming after the string (ie., 'IncludeDateTime = ' ) in the same line. I tried to write this script in... (5 Replies)
Discussion started by: jannusuresh
5 Replies

5. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

6. UNIX for Dummies Questions & Answers

How to delete one line from a text file

PATTERN="" sed "/$PATTERN/d" $file In the file data is stored as ::: ::: I need to delete only the line containing the PATTERN pls help to point out why the above code is not working (4 Replies)
Discussion started by: thomsandy
4 Replies

7. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

8. UNIX for Dummies Questions & Answers

how to delete line with matching text and line immediately after

hello experts, I have a file: File1 Sample Test1 This is a Test Sample Test2 Another Test Final Test3 A final Test I can use sed to delete the line with specific text ie: sed '/Test2/d' File1.txt > File2.txt How can I delete the line with the matching text and the line immediately... (6 Replies)
Discussion started by: orahi001
6 Replies

9. Shell Programming and Scripting

Delete first line from any text file ?

What command I can use to delete first line from any text file ? Thk (5 Replies)
Discussion started by: aungomarin
5 Replies

10. Shell Programming and Scripting

delete last line from text file

I have a file with which i must remove the last line of the file (the footer). I know how to copy and redirect with tail -1 etc, but how do i delete it permanentely (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question