deleting a line but keeping the same file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers deleting a line but keeping the same file
# 1  
Old 05-13-2008
deleting a line but keeping the same file

Hi,

I want to delete a line in a file that contains a string. I tried:
grep -v "mystring" Myfile > Myfile

But this makes the Myfile empty. I read that I need to do something like:
grep -v "mystring" Myfile > Myfile.new
rm Myfile
mv Myfile.new Myfile


Is there a way to avoid creating a temporary file??

Thanks in advance.
# 2  
Old 05-13-2008
Hi Laiko,


There is no such way.


Regards,
Vinod.
# 3  
Old 05-13-2008
If you have GNU sed, which supports "-i" option, you can use:

Code:
sed -i "/mystring/d" Myfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Deleting the last non blank line from a file

PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17... (6 Replies)
Discussion started by: swathi reddy1
6 Replies

2. Shell Programming and Scripting

Issue deleting blank line from a file

I'm trying to delete the blank lines from the file $Sfile. tried the below set of commands. Both are giving the same error (: bad interpreter: No such file or directory) awk 'NF > 0' $Sfile > $Tfile cat $Tfile sed -i '/^$/d' $Sfile cat $Sfile Not sure if there's any other problem with... (17 Replies)
Discussion started by: Pandee
17 Replies

3. Shell Programming and Scripting

Deleting Line number from different file

file 1 abcde,dfgdfg,sdfds sdfsd,sdfd,sdfg,sdfs dwfds,sdfgdfg,sdfsdf file2 1 3 i need to delete the line numbers file 1 based on file2 i have taken in loop file 1 and i cant able to delete that line number in file 1 Plz help (4 Replies)
Discussion started by: ragu.selvaraj
4 Replies

4. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

5. Shell Programming and Scripting

Insert a line in a file by deleting that line from another file

Hi all, I have a huge file(size more that 5GB). I want to do some manupulation with the records and write to another file. As the size of the file is huge and there is a space constraint in that directory, I want to delete that record from first file after writing it in to second file.... (3 Replies)
Discussion started by: gani_85
3 Replies

6. Shell Programming and Scripting

Deleting repeated lines by keeping only one.

Dear Buddies, Need ur help once again. I have a flat file with around 20 million lines (Huge file it is). However, many of the lines are of no use hence I want to remove it. To find and delete such lines we have certain codes written at the starting of each line. Basis that we can delete the... (2 Replies)
Discussion started by: anushree.a
2 Replies

7. UNIX for Dummies Questions & Answers

Deleting the first line of .gz file

Hi All, I have too many .gz files (test.gz). Task is to remove first line of each file. Can I do it without unzipping the files? Your help is appreciated. (4 Replies)
Discussion started by: Chulamakuri
4 Replies

8. Shell Programming and Scripting

Keeping only 6 logs and deleting the rest...

Hi Guys, I have folder where the logs are generated (which is quite a few). I want to keep the recent n logs and delete the rest of them.. I am able to sort them by using ls -t.. but how to take it forward.. Can you people help me.. Thanks for the help in advance, Regards,... (3 Replies)
Discussion started by: mac4rfree
3 Replies

9. UNIX for Dummies Questions & Answers

Sort and uniq lines of a file while keeping a header line

So, I have a file that has some duplicate lines. The file has a header line that I would like to keep at the top. I could do this by extracting the header from the file, 'sort -u' the remaining lines, and recombine them. But they are quite big, so if there is a way to do it with a single... (1 Reply)
Discussion started by: Digby
1 Replies

10. UNIX for Dummies Questions & Answers

Deleting all content in a file from command line

Hey guys, I'd like to delete all text within a text file without actually deleting the file. Is there a vi command I can do from my prompt without actually going into the file and doing a %d i think it is... to remove all the text inside? I want to add this to a cron job to erase all the data... (2 Replies)
Discussion started by: kingdbag
2 Replies
Login or Register to Ask a Question