BearPanther.com

Searching files and deleting a line that contains some term

Introduction

user

bear


LATEST POSTS

Using jsformat with Sublime Text 26th July, 2014

There are many d3 demos like this but this one is mine 08th June, 2014

Blog

Searching files and deleting a line that contains some term

Posted on .

How would you delete a line from one or more of hundreds of files in a tree of directories when a line contains a specific term? When this was put in front of me, I thought, “np, sublime can probably handle this”. As great as our text editors are, I don’t think this is a task they’re up to (except vim, there’s always a way in vim).

I didn’t want to go the route of writing code to solve this. My linux-fu is rusty at best, but I figured I could use sed or awk or one of those decades-old bulletproof unix utilies. After some quick googling/stackoverflowing, I came across an answer to Sed – Delete a line containing a specific string. Looks like that covers the “delete a line” part.

Now how about the “in a tree of directories”? Stackoverflow to the rescue again.

When I combined the two, I came up with:

find . -type f -iname *.html -exec sh -c 'sed -n "/lines-with-this-text-will-be-deleted/!p" {} > temp && mv temp {}' ;

I also used a find command tutorial for help with the -exec piece.

Comments
user

Author Starly

Posted at 1:10 pm August 18, 2014.

Heck of a job there, it abutsloely helps me out.

Reply

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

View Comments (1) ...