delete line from file if successful partial string found


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users delete line from file if successful partial string found
# 1  
Old 05-06-2009
delete line from file if successful partial string found

Id like to delete a line from a file using (preferably a single line unix command) if it contains a certain string pattern.

If line contains "abcdef" then delete that line.
Help greatly appreciated.
# 2  
Old 05-06-2009
Code:
# grep -v "abcdef" file > newfile

# 3  
Old 05-07-2009
sed -i "/abcdef/d" <filename>. this will remove the abcdef from the existing file. (take backup of the existing file before removing , if required)
# 4  
Old 05-12-2009
Quote:
Originally Posted by apsprabhu
sed -i "/abcdef/d" <filename>. this will remove the abcdef from the existing file. (take backup of the existing file before removing , if required)
I know that. I want to say if abcdef is in the line then delete the ENTIRE line.
Anyone know how I can do that?

I need to do a huge number of files so while Im grateful for the first reply I cant go with it.
# 5  
Old 05-12-2009
Quote:
Originally Posted by cronjob78
I know that. I want to say if abcdef is in the line then delete the ENTIRE line.
Anyone know how I can do that?
have you tried the proposed code?
Did it work? If not, what errors are you getting?
Quote:
Originally Posted by cronjob78
I need to do a huge number of files so while Im grateful for the first reply I cant go with it.
Why is that?
# 6  
Old 05-12-2009
Quote:
Originally Posted by vgersh99
have you tried the proposed code?
Did it work? If not, what errors are you getting?

Why is that?
I dont want to use the first one because I will need to run a command on on thousands of files. The second command didnt work. It didnt like the 'd' at the end of the command.

If you must know its because a trojan filled them with large lines of code. There are a few patterns it left so Im hoping to delete them all in one go.
The lines are painstakingly long so i cant just implement a sed command
# 7  
Old 05-12-2009
Quote:
Originally Posted by cronjob78
I dont want to use the first one because I will need to run a command on on thousands of files.
so why don't you put the command in a loop - processing one file at a time?
Quote:
Originally Posted by cronjob78
The second command didnt work. It didnt like the 'd' at the end of the command.
strange - worked fine here. I don't the GNU sed with '-i' implemented, but the standard 'sed' did the job.
Quote:
Originally Posted by cronjob78
If you must know its because a trojan filled them with large lines of code. There are a few patterns it left so Im hoping to delete them all in one go.
The lines are painstakingly long so i cant just implement a sed command
Why cannot you implement 'sed'? Have you tried it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

3. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

4. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

5. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

8. Shell Programming and Scripting

Delete File Only If Successful Transfer..

Hi. I have this shell script for ftp.. ftp -n 12.34.5.67 << EOF user username password cd LOCAL/inbox bin get JAN_Total.gz # del JAN_Total.gz EOF how do i modify the commented part i.e. del JAN_Total.gz only if that JAN_Total.gz has been successfully transfered to the local... (8 Replies)
Discussion started by: aimy
8 Replies

9. Shell Programming and Scripting

Delete original wav file if lame was successful encoding.

In a bash script: src=”cooltrack.wav” dst=”cooltrack.mp3” lame $src $dst I would like to add some line that would delete the source wav file like: rm $src but I would like this only if the encoding was successful. What should I include before deleting the original to check that the... (2 Replies)
Discussion started by: Aia
2 Replies

10. Shell Programming and Scripting

Need to delete previous line after successful seearch

i have a file in following pattreen A: Size = 10 B: Size = 0 C: Size = 220 D: Size = 0 i want to Display only Charecters which have which have Size = 0 The Out put Should be B: D: Can Some one Help (7 Replies)
Discussion started by: pbsrinivas
7 Replies
Login or Register to Ask a Question