-3

I want a universal way of displaying all the lines in grepFile that do not start with a letter.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

3 Answers3

3

Easy enough: grep -v '^[[:alpha:]]' testfile

with a testfile like this one:

$ cat testfile 
sfdsaf
1sdflad
 asfd
,asdfasdf
safdaf

The above command will return:

$ grep -v '^[[:alpha:]]' testfile 
1sdflad
 asfd
,asdfasdf

Why?

  • ^ greps the beginning of a line
  • [[:alpha:]] greps for 1 letter
  • -v inverts the match

An alternative would be using sed '/^[[:alpha:]]/d' testfile, where sed's d command deletes everything that matches ^[[:alpha:]] --which again is a letter at the start of the line.

Another alternative is awk '/^[^a-zA-Z]/ { print $0 }' testfile

markgraf
  • 2,860
0

Simply use the regex option in grep and inverse the output of finding a letter as the first character:

$ cat file.txt

asd
Ajnjn
bdfadsf
Bkkkknm
csdgadga
4gafga
dfsdf
esdfasdg
2fsefse
~asdasd
 dfdfsd
fartaherh
3adsfasdf

$ grep -ve '^[a-zA-Z]' file.txt

4gafga
2fsefse
~asdasd
 dfdfsd
3adsfasdf
Smock
  • 125
  • 5
  • ^ is special in many shells (Bourne, fish, rc, es, zsh -o extendedglob) and would need to be quoted there. – Stéphane Chazelas Sep 07 '19 at 10:30
  • [...] is special in all shells but fish. For instance, in bash if there were files called ^a and ^Z, that ^[a-zA-Z] would expand to both filenames. Again, you'd want to quote it. – Stéphane Chazelas Sep 07 '19 at 10:30
  • What character or sequence of character (collating element) [a-zA-Z] matches varies with the locale and is probably not what you want outside of the C locale. You'd want to use LC_ALL=C grep -v '^[a-zA-Z]' to match only on US-ASCII English letters (though if the file.txt was encoded in some multibyte charset, it could end-up match parts of some characters), or grep -v '^[[:alpha:]] to match on any alphabetic collating element in your locale. – Stéphane Chazelas Sep 07 '19 at 10:34
  • All very true, and in that case this is the answer that should be marked correct, and I'm upvoting for taking a lot more into account. – Smock Sep 09 '19 at 14:09
-1

You can try with below command too

command1:

sed -n '/^[a-zA-Z]/!p'  testfile

command2:

awk '$0 !~ /^[a-zA-Z]/{print $0}' testfile
  • 1
    What about upper-case letters? Note also https://unix.stackexchange.com/a/418247/117549 and https://unix.stackexchange.com/a/105784/117549 – Jeff Schaller Sep 05 '19 at 10:45