I'm looking for grep to show all characters which do not starts with numbers. I have done something like this:

grep -v '^[1-2]*[a-zA-Z]?' -o

but it do not work. Do you have any idea for some reg exp?

  • 1
    Using -v with -o causing grep produce no output. – cuonglm Feb 25 '15 at 16:43
  • What do you mean by show all characters? You mean lines which do not start with numbers, or do you want to strip out all the numbers from the input? – Matteo Feb 25 '15 at 16:43
  • By default, grep uses basic regular expressions. This means that your ? is being treated as a literal question-mark. Either escape the question-mark \?, or use the -E option for extended regular expressions, in which case ? is a pattern character. – Peter.O Feb 25 '15 at 17:25

grep -v '^[0-9]'

Will output all the lines that do not (-v) match lines beginning ^ with a number [0-9]

For example

$ cat test
$ grep -v '^[0-9]' test

or if you want to remove all the words that begin with a digit

sed 's/[[:<:]][[:digit:]][[:alnum:]_]*[[:>:]]//g'

or with shortcuts and assertions

sed 's/\<\d\w*\>//g'

For example

$ cat test
five six
seven 8eight
9nine ten
11eleven 12twelve
a b c d
$ sed 's/[[:<:]][[:digit:]][[:alnum:]_]*[[:>:]]//g' test

five six

a b c d
  • @mikeserv why? It removes every word beginning with a digit (as maybe asked in the original question) – Matteo Feb 26 '15 at 6:52
  • 1
    Nope it does work. End on OS X (and maybe other BSDs) \> and \< are not working. For this reason I mentioned both versions. And I had to use the ugly one on my machine for the example. – Matteo Feb 26 '15 at 10:56
  • From the man page \> Matches the null string at the end of a word. This is equivalent to [[:>:]]'.` – Matteo Feb 26 '15 at 10:56

It depends how do you define a string (e.g. if you count punctuation characters to string or not). Nevertheless you may start from something like

grep -Po '\b[^[:digit:]].*?\b' file

To remove all words from a line that begin with a number with sed you can do:

sed 'x;s/.*//;G

...or, if you wanted only words which do not begin with numbers printed each on a separate line:

sed 'y/!\t "'"'?/\n\n\n\n\n\n/;/^[_[:alpha:]]/P;D"

...the above should do fairly well. You'll want to tailor the \newline y///translation for dividers you think are relevant. And, sed implementation depending, you might also want an actual <tab> in place of the \t backslash escape.

  • @don_crissti - Oh, yeah - I'll put that ! ahead of the '"' swap, but if you're using bash you might want set +H or if zsh then set -K. In my opinion, any quoted ! expansion is insanity. You can also use heredocs like "${0#-}" <<\CMD\nyour cmd strings\nCMD\n to get scripted behavior in interactive shells. – mikeserv Feb 26 '15 at 18:23
  • Thanks for the "${0#-}" <<... tip ! – don_crissti Feb 26 '15 at 18:29
  • @don_crissti - If you use "${0#-}" -s -- arg list <<\CMD\n... you can also set the positional parameters at invocation. Using "$@" or * is often useful for me in place of arg list. And with ln -s "$(command -v "${0#-}")" /tmp/new_name; cd tmp; new_name <<\CMD\n... you can get a new $0 and still handle stdin. – mikeserv Feb 26 '15 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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