0

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.

ananthan
  • 1,510
  • 1
  • 18
  • 28
  • Can you guarantee that each sentence will be on a single line, ie, that no matching sentence will contain a line break? – MadHatter Dec 05 '12 at 09:43
  • @MadHatter yes all are on a single line. – ananthan Dec 05 '12 at 09:45
  • 1
    For the future, simple regex/`sed` questions like this are really better suited to [unix.se], 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.SE] got created we like to encourage people to use it :) – voretaq7 Dec 06 '12 at 03:03
  • @voretaq7 thanx...next time i will use that... – ananthan Dec 06 '12 at 03:38

3 Answers3

4

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.*;//' 
MadHatter
  • 79,770
  • 20
  • 184
  • 232
  • Asker wants to delete the line - your solution will just make it blank. – Jay Dec 05 '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 05 '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 05 '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 05 '12 at 13:00
2

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;
Jay
  • 6,544
  • 25
  • 34
  • i dont want to replace content instead delete that line starts with that string – ananthan Dec 05 '12 at 09: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 05 '12 at 09:59
2

You can use sed:

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

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

mulaz
  • 10,682
  • 1
  • 31
  • 37