# delete line from file if successful partial string found

**URL:** <https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [May 6, 2009, 8:49am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433 "2009-05-06T08:49:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cronjob78](https://community.unix.com/letter_avatar/cronjob78/32/5_5575768a8748004e209b776fc1b2916d.png) [@cronjob78](https://community.unix.com/u/cronjob78)\
**Post date:** [May 6, 2009, 8:49am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/1 "2009-05-06T08:49:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [May 6, 2009, 9:17am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/2 "2009-05-06T09:17:40Z")

</div>

```nohighlight
# grep -v "abcdef" file > newfile

```

---

<div class="post-metadata">

**Author:** ![apsprabhu](https://community.unix.com/letter_avatar/apsprabhu/32/5_5575768a8748004e209b776fc1b2916d.png) [@apsprabhu](https://community.unix.com/u/apsprabhu)\
**Post date:** [May 7, 2009, 7:57am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/3 "2009-05-07T07:57:05Z")

</div>

sed -i "/abcdef/d" \<filename\>. this will remove the abcdef from the existing file. (take backup of the existing file before removing , if required)

---

<div class="post-metadata">

**Author:** ![cronjob78](https://community.unix.com/letter_avatar/cronjob78/32/5_5575768a8748004e209b776fc1b2916d.png) [@cronjob78](https://community.unix.com/u/cronjob78)\
**Post date:** [May 12, 2009, 9:58am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/4 "2009-05-12T09:58:29Z")

</div>

> [@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.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [May 12, 2009, 10:34am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/5 "2009-05-12T10:34:43Z")

</div>

> [@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?

> [@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?

---

<div class="post-metadata">

**Author:** ![cronjob78](https://community.unix.com/letter_avatar/cronjob78/32/5_5575768a8748004e209b776fc1b2916d.png) [@cronjob78](https://community.unix.com/u/cronjob78)\
**Post date:** [May 12, 2009, 11:07am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/6 "2009-05-12T11:07:42Z")

</div>

> [@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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [May 12, 2009, 11:12am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/7 "2009-05-12T11:12:53Z")

</div>

> [@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?

> [@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.

> [@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?

---

<div class="post-metadata">

**Author:** ![apsprabhu](https://community.unix.com/letter_avatar/apsprabhu/32/5_5575768a8748004e209b776fc1b2916d.png) [@apsprabhu](https://community.unix.com/u/apsprabhu)\
**Post date:** [May 14, 2009, 2:35am UTC](https://community.unix.com/t/delete-line-from-file-if-successful-partial-string-found/239433/8 "2009-05-14T02:35:45Z")

</div>

> [@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?
> 
> I need to do a huge number of files so while Im grateful for the first reply I cant go with it.

Hi Cronjob78,

Have to tried this command " sed -i "/abcdef/d" \<filename\>. " ???. This command not only removes the string adcdef, this will remove the entire line. I beleive i have posted this command only after testing.  
------------------------------------------  
See the below example......

-sh-3.1$ grep ABC aps\*  
aps1.txt:ABC  
aps1.txt:ABC123  
aps1.txt:123ABC  
aps1.txt:ABCDEFGH  
aps1.txt:ABCDE123  
aps1.txt:123456789ABC  
aps2.txt:ABCDEFGHIJKLMNOP  
aps2.txt:PRABHUABC  
aps2.txt:SHANKAR ABC  
aps2.txt:--ABC---

-sh-3.1$ sed -i "/ABC/d" aps\*txt

-sh-3.1$ grep ABC aps\*
