A
A
Alexander Semenenko2019-03-11 13:22:49
linux
Alexander Semenenko, 2019-03-11 13:22:49

How can I filter the output by the number of characters in a column?

Good afternoon.
I am writing a script and needed to do the following. There is a file like this:
102 xxxxx xxxxxxxxxxx
150 xxxxx xxxxxxxxxxx
3000 xxxxx xxxxxxxxxxx
3600 xxxxx xxxxxxxxxxx
In general, the first columns can be 3-digit numbers or 4-ex. Here's how I can display only those lines in which there are 4 digits in the first column?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Viktor Belsky, 2019-03-11
@semenenko88

egrep '^[0-9]{4}' /your/file/is/here

M
Mrrusik, 2019-03-11
@Mrrusik

cat file | grep '^....'

K
ky0, 2019-03-11
@ky0

The dumbest way is to compare the first column with 1000 in a loop or, if options with leading zeros are possible, count the number of characters (see ${#VAR}).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question