12

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?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 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
  • http://unix.stackexchange.com/questions/60994/how-to-grep-lines-which-does-not-begin-with-or – Ciro Santilli OurBigBook.com Jul 27 '15 at 07:31

3 Answers3

23

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
string
string123
123string
1string2
$ grep -v '^[0-9]' test
string
string123

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
one
two2
3three
4four4
five six
seven 8eight
9nine ten
11eleven 12twelve
a b c d
$ sed 's/[[:<:]][[:digit:]][[:alnum:]_]*[[:>:]]//g' test
one
two2


five six
seven 
 ten

a b c d
Matteo
  • 9,796
  • 4
  • 51
  • 66
  • @mikeserv why? It removes every word beginning with a digit (as maybe asked in the original question) – Matteo Feb 26 '15 at 06: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
3

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
jimmij
  • 47,140
2

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

sed 'x;s/.*//;G
     s/[[:space:]][[:punct:]]\{0,1\}[0-9][^[:space:]]*//g
     s/\n//'

...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.

mikeserv
  • 58,310
  • @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