Issue
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?
Solution
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.
Answered By - Wiktor Stribiżew