Betafish
Betafish

Reputation: 1262

awk command to delete rows if it has number as the first character of first column in a tsv file

I am trying to delete rows if it has number as the first character in the first column in a tsv file.

abc london
def paris
4ge berlin
323 rome
xyz prague
#56 Bern

I would like to delete (rows: 4ge, 323,#56). What I tried is something like this

awk 'BEGIN{FS=OFS="\t"}{$1=/^[0-9]/; print $1,$2}' train.tsv > train.tsv

Upvotes: 0

Views: 274

Answers (2)

Ed Morton
Ed Morton

Reputation: 203209

That's the kind of job grep was invented to do:

grep -v '^[0-9]'

The difference between that and grep '^[^0-9]' is that the former will print blank lines while the latter won't.

Upvotes: 2

hek2mgl
hek2mgl

Reputation: 157947

Like this:

awk '!/^[[:digit:]]/' file

Since it is the first column you can simply check if the whole line starts (not) with a number. If you would be interested in any other column you can use (for example second column):

awk '$2 !~ /^[[:digit:]]/' file

Btw, you may miss the print command here. That's because it is default command, meaning

awk '!/^[[:digit:]]/' file

is the same as

awk '!/^[[:digit:]]/{print}' file

which is the same as

awk '!/^[[:digit:]]/{print $0}' file

Upvotes: 3

Related Questions