L
L
lemonlimelike2020-05-12 14:34:39
linux
lemonlimelike, 2020-05-12 14:34:39

How to display the desired lines through grep?

Hello! I am training to use grep, I found a couple of tasks. Can't print correct lines.
Here is the file itself 5eba85de1b412549953319.png
Here is the structure: 5eba869c9204f962925509.png
Here is the text:

7954 JAMES     T MURRAY    CLERC       SALES      DALLAS   750
7950 ALICE     B JENSEN    CLERC       SALES      NEW-YORK 750
7934 BARBARA   M MILLER    CLERC       ACCOUNTING NEW-YORK 1300
7919 MICHAEL   A DOUGLAS   CLERC       RESEARCH   NEW-YORK 800
7916 GRACE     M ROBERTS   ANALYST     RESEARCH   NEW-YORK 2875
7900 FRED      S JAMES     CLERC       SALES      CHICAGO  950
7698 MARION    S BLAKE     MANAGER     SALES      CHICAGO  2850
7676 DENISE    D SOMMERS   STAFF       OPERATIONS CHICAGO  1850
7654 KENNETH   J MARTIN    SALESPERSON SALES      CHICAGO  1250
7609 RICHARD   M LEVIS     STAFF       OPERATION  DALLAS   1800
7600 RAYMOND   Y PORTER    SALESPERSON SALES      NEW-YORK 1250
7569 CHRIS     L ALBERTS   MANAGER     RESEARCH   NEW-YORK 3000
7566 TERRY     M JONES     MANAGER     RESEARCH   DALLAS   2985
7564 GREGORY   J LANGE     SALESPERSON SALES      DALLAS   1250
7560 SARAH     S DUNCAN    SALESPERSON SALES      DALLAS   1250
7557 KAREN     P SHAW      SALESPERSON SALES      NEW-YORK 1250
7555 DANIEL    T PETRS     SALESPERSON SALES      NEW-YORK 1250
7369 JONH      Q SMITY     CLERK       RESEARCH   DALLAS   800
7499 KEVIN     J ALLEN     SALESPERSON SALES      CHICAGO  1600
7505 JEAN      K DOYLE     MANAGER     SALES      NEW-YORK 2800
7506 LYNN      S DENNIS    MANAGER     SALES      DALLAS   2750
7507 LESLIE    D BAKER     MANAGER     OPERATION  NEW-YORK 2200
7521 CYNTHIA   D WARD      SALESPERSON SALES      CHICAGO  1250
7782 CAROL     F CLARK     MANAGER     ACCOUNTING NEW-YORK 2450
7788 DONALD    T SCOTT     ANALYST     RESEARCH   DALLAS   3000
7789 LIVIA     N WEST      SALESPERSON SALES      DALLAS   1300
7799 MATTHEW   G FISHER    ANALYST     RESEARCH   NEW-YORK 3000
7820 PAUL      S ROSS      SALESPERSON SALES      BOSTON   1300
7839 FRANCIS   A KING      PRESIDENT   ACCOUNTING NEW-YORK 5000
7876 DIANE     G ADAMS     CLERK       RESEARCH   DALLAS   1100
7902 JENNIFER  D FORD      ANALYST     RESEARCH   DALLAS   3000
7919 MICHAEL   A DOUGLAS   CLERK       RESEARCH   NEW-YORK 800


The first task is to select all rows where the salary is at least 1000. This is how I did it
grep '^.\{59\}[0-9]\{1,\}[0-9][^0-9]\{0,1\}' query1

but they display lines in which the salary is less than 1000, but you need more, how to do this?
And the second task is to select all rows where the first initial is not 'K' and the second is not 'J'. It turned out only when the first initial is 'K' and the second is 'L'
grep "^\S*\s*K.*\sJ\s*.*" query1
, that is, you need to put a negation. How?
And the third task is to select all rows where the name is 'PALL' and the city is 'BOSTON'. It didn't work out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2020-05-12
@lemonlimelike

I have a feeling that the output is received by a SQL query. If so, then filtering should be done with an SQL query, and not perverted with grep - because grep is forced to parse the fields again.
The second option is to work with something like AWK that splits lines into fields. True, there might be. problems with empty fields that he will not see. 1) " Salary of 1000 and
above" = "four or more digits" = [0-9]{4,}or more precisely , except for the range {from "K" to "P"} 3) If you need to work with fields, then, as I said, grep works very badly. Option from mureevms[1-9][0-9]{3,}
[^K]
[^KJ]
[^K-P]
'PAUL.+BOSTON'the bad thing is that it will work if these values ​​appear in the "last name" field, which they can.
Even worse: this option will also accept the name "CO PAUL ER" (thought up from a finger, purely for example).
I can strain and write a more correct regex for this case - well, for starters, enclose these words with spaces. But I won't because I'm lazy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question