How to delete one line from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to delete one line from a text file
# 1  
Old 01-29-2010
How to delete one line from a text file

Code:
PATTERN="[cde]"
sed "/$PATTERN/d" $file

In the file data is stored as
Code:
[cde]:[123]:[1]:[2]
[abc]:[456]:[1]:[5]

I need to delete only the line containing the PATTERN [cde]

pls help to point out why the above code is not working

Last edited by Yogesh Sawant; 02-26-2010 at 01:30 PM.. Reason: added code tags
# 2  
Old 01-29-2010
Try:
Code:
PATTERN="\[cde\]"

or use:
Code:
PATTERN="[cde]"
fgrep -v "$PATTERN" "$file"

# 3  
Old 02-24-2010
Dear Friend,

If you want to affect the file, in the "sed" script you should use -i option while execute the command.

consider the following contents are there in the file
[cde]:[123]:[1]:[2]
[abc]:[456]:[1]:[5]

Code:
sed -i '/cde/d'  file

Now the file is contains only the following contents

[abc]:[456]:[1]:[5]
# 4  
Old 02-24-2010
Hello,

Code:
var=cde
grep -v "$var" infile > newfile

or

Code:
sed -i.bak "/$var/d" infile

this fixes the file in place producing a backup file by name infile.bak

Regards,
Gaurav.
# 5  
Old 02-24-2010
U can also use :

grep -v "cde" filename
 
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

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 (6 Replies)
Discussion started by: Berserk
6 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