D Parker
D Parker

Reputation: 21

regex exclude lines with first word ending in colon

I'm trying to strip out lines that have the format

word: bla bla
anotherword: bla bla

but I want to keep

this example: bla bla

I tried this: cat "myfile" | grep -v "\w: "

but that removes all lines with any word: combination

if I do this cat "myfile" | grep -v "^\w: "

to say, only lines where the pattern is at the beginning, it doesn't exclude any of the lines

I also tried cat "myfile" | sed '/^\w:/d' but that didn't work either

if I try it with a specific word like cat "myfile" | sed '/^title:/d'

that works the way its supposed to.

What am I doing wrong?

Upvotes: 1

Views: 111

Answers (2)

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed -E '/^\w+:(\s|$)/d' file

This would strip out lines of single words ending in a colon only or single words ending in a colon followed by a white space.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

You may use

grep -vE '^[_[:alnum:]]+:' file

It matches any lines but (due to -v option) those that

  • ^ - start with
  • [_[:alnum:]]+ - 1 or more alphanumeric or _ chars
  • : - a colon.

Note that \w, which may be replaced with [_[:alnum:]] if supported, just matches a single word character.

Upvotes: 1

Related Questions