3

How would I generate a list that displays the lowest numbers in a file?

ex.

  1. List item 2476 Jackie Woodson :manager: :sales: :05/01/59: 110000
  2. List item 2000 Jack Wilson :director: :accounts: :04/19/55: 80000
  3. List item 7700 Wilson Ali :executie: :accounts: :08/30/56: 90000
  4. List item 3364 Bill Wilcocks :manager: :sales: :12/12/55: 65000

  5. List item 4290 Neil O'bryan :executive: :accounts: :05/01/59: 100000

  6. List item 2000 Wilson Albert :Chairman: :productions: :03/12/50: 125000

I want to list the show the range between 60000 and 9000 and list only those using grep

I've tried

grep "[69]" .....$ filename

2

grep does not do numeric comparisons, and is not the tool to use here. I'd use:

sort --field-separator=":" --numeric-sort --reverse --key=7 --ignore-leading-blanks filename | \
    tail --lines=3  | \
    sort --field-separator=":" --numeric-sort --key=7 --ignore-leading-blanks
1

grep is line matching tool, which means it only matches lines which has specific text. What you want is text processing utility, such as awk that can do comparisons.

$ awk -F ':' '$NF > 60000 && $NF<90000' list.txt                                  
List item 2000 Jack Wilson :director: :accounts: :04/19/55: 80000
List item 3364 Bill Wilcocks :manager: :sales: :12/12/55: 65000

The way this works is simple:

  • -F ':' tells awk to treat each line as set of words separated by colons
  • '$NF > 60000 && $NF<90000' will look at the last word $NF , and check if it's greater than 60000 AND (the && part) 90000.
0
grep -P '\b[6-8]\d{4}\s*$' ex3
  • 1
    -P, --perl-regexp Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented features. – Elder Geek Nov 8 '16 at 18:46
  • @ElderGeek, in fact this option is present just in some grep implementations such as gnugrep -- the grep available in ubuntu. Although experimental this option in available for years and is very robust and widely used – JJoao Nov 8 '16 at 20:09
  • Do you think that information might be suitable to edit into your answer? (comments can be deleted for a number of reasons) – Elder Geek Nov 8 '16 at 20:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.