Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having 100's of files that contain sentence that starts with mark and ends with semicolon(;).

eg: mark is driving a car;

i need to remove every sentence starts with mark and end with ";"

How to do this using sed or simmilar tools.

i have used sed to replace pattern but dont know how to delete patterns.

share|improve this question
Can you guarantee that each sentence will be on a single line, ie, that no matching sentence will contain a line break? – MadHatter Dec 5 '12 at 9:43
@MadHatter yes all are on a single line. – ananthan Dec 5 '12 at 9:45
For the future, simple regex/sed questions like this are really better suited to Unix & Linux, unless they're in some way related to professional system administration (per the SF FAQ). I know it seems like a strange/arbitrary distinction, but since Unix & Linux got created we like to encourage people to use it :) – voretaq7 Dec 6 '12 at 3:03
@voretaq7 time i will use that... – ananthan Dec 6 '12 at 3:38
up vote 4 down vote accepted

Since all matching sentences are all on a single line, and you say you're already familiar with sed, then it's just a special case of using sed to replace one pattern with another, in this case replacing with nothing:

sed -e 's/mark.*;//' 
share|improve this answer
Asker wants to delete the line - your solution will just make it blank. – Jay Dec 5 '12 at 12:20
That's not what he asked; he asked to remove every matching sentence, not to delete every line on which such a sentence occurs. I agree that, from his comment on your answer, the latter appears to be what he wants, but it's not what he asked for! – MadHatter Dec 5 '12 at 12:38
I suppose that is open to interpretation: i have used sed to replace pattern but dont know how to delete patterns. I read that is he is OK with find/replace, but not find/delete. – Jay Dec 5 '12 at 12:57
Fair enough. As you can tell from my answer, I read it as "I know how to replace A with B, but I have not realised that deleting string A is equivalent to replacing it with ''". There is also the unaddressed issue of what the OP wants done with a line that says steve is driving a bus; mark is driving a car;. – MadHatter Dec 5 '12 at 13:00

You can use the d in sed to delete a line:

$ echo -e "mark is driving a car;\nbob is driving a car;" | sed -e '/^mark/d'
bob is driving a car;
share|improve this answer
i dont want to replace content instead delete that line starts with that string – ananthan Dec 5 '12 at 9:56
Which is exactly what my example shows? mark is driving a car; is no longer in the output, but bob is driving a car; is (the \n in the echo means "new line"). – Jay Dec 5 '12 at 9:59

You can use sed:

sed -e '/^mark.*;$/d'

This removes everything starting with "mark" and ending with ";".

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.