wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Grep.Need help with finding the words which start at [A-K] letters in the third column of the table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Grep.Need help with finding the words which start at [A-K] letters in the third column of the table" Watch "Grep.Need help with finding the words which start at [A-K] letters in the third column of the table" New topic
Author

Grep.Need help with finding the words which start at [A-K] letters in the third column of the table

Vaidas Kve
Greenhorn

Joined: Apr 27, 2012
Posts: 2
Hi buddies ! I need some help with one grep command I have this table:

1 Petras Pavardenis 1980 5
08 Linas Bajoriunas 1970 10
3 Saulius Matikaitis 1982 2
5 Mindaugas Stulgis 1990 7
6 Rimas Nasickis 1964 10
7 Simas Saulenis 1966 12
8 Justas Skukauskas 1995 9
9 Petras Petronkus 1988 3
10 Zanas Baltraitis 1990 5
11 Mantas Tutkus 1974 1
12 Edmundas Butkus 1970 9
13 Mindaugas Sinkevicius 1945 11
14 Edgaras Zvironas 1930 8
15 Vaidas Gaubas 1985 12
16 Gintaras Gintaitis 1980 5
17 Raigardas Tautkus 1975 1
18 Darvydas Tautkevicius 1925 12
19 Tomas Cereska 1989 4
20 Saulius Sidlauskas 1984 6
21 Jonas Jonaitis 1966 8
22 Jonas Petraitis 1940 12
23 Jonas Ringys 1970 1
13 Julius Jonaitis 1974 11

And i need to find the words which start with capital letters A,B,C,D,E,F,G,H,I,J,K in the column 3 (the first word of this column is "Pavardenis") . Not in all columns but just in the third column (as I said earlier the first word of this column is "Pavardenis"). And i must use only GREP command (I cannot use awk or other commands due to my school restrictions).
alex yarm
Greenhorn

Joined: Apr 28, 2012
Posts: 5
just use a grep -e with regular expressions?
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Please see this: NotACodeMill
Show us what you have so far and tell us what results you are getting so far. We will then be happy to answer specific questions or offer specific pointers.


JBoss In Action
Vaidas Kve
Greenhorn

Joined: Apr 27, 2012
Posts: 2
I found the solution. This is the solution of my problem: command: egrep '^.{4}.*[A-K]' filename number 4 means the amount of ignored spaces (columns) including tabs and space Thanks for your help
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Try awk (or its variants)

awk '$3 ~ /^[A-K]/' {print}' file

Methinks the command line is readable to an extent that it almost paraphrases your subject (except that it uses awk instead of grep).


Also, hope you are aware that your grep solution uses the greedy .* that follows a .{4}. I suppose that works for your input. A slightly better (i.e., more expressive) grep solution would be

grep '^[^A-Z]*[A-Z].*[A-K]' file

It is basically looking for the string that has one (any) upper case letter followed by one or more arbitrary character(s) followed by one upper case between A through K.

[Edit: Sorry, didn't realise (1) this was homework (2) that you were precluded from using awk. Peter's response was the correct one.]


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
 
 
subject: Grep.Need help with finding the words which start at [A-K] letters in the third column of the table