1

I am using grep to extract some exact numbers in column #2 of a long file.

I am using the following code:

grep  -e "^71161 |^71072 |^72617 " mainfile > outputfile

This does nothing in outputfile

Please help.

My file is like this:

Date        ID  STN     SUPER   LAT     LONG    OBS     VAR
2014060106  71072   146 S000438 48.37   270.68  2   0   
2014060106  71108   12  71108   49.03   237.63  0   0.04
2014060212  71108   12  71108   49.03   237.63  0   0.16    
2014060212  71120   12  71120   54.4    249.73  0   0   
2014060212  71123   12  S000400 53.32   246.42  0   0.11    
2014060212  71125   12  S000961 54.13   251.48  0.05    0.00
2014060212  71140   12  S000388 49.92   260.05  2   0.21    
2014060212  71150   146 71150   50.45   259.4   1   2.21    
  • 1
    Please add sample input and your desired output for that sample input to your question. – Cyrus Dec 6 '15 at 21:06
  • 3
    It is obvious why it does not work, ^ is for beginning of line, it will never match the 2nd column. – Rui F Ribeiro Dec 6 '15 at 21:07
  • Date ID STN SUPER LAT LONG OBS VAR 2014060106 71072 146 S000438 48.37 270.68 2 0 2014060106 71108 12 71108 49.03 237.63 0 0.0448 – Batchguy Dec 6 '15 at 21:40
2

awk may be a better choice here:

awk 'BEGIN{a[71161] a[71072] a[72617]}; $2 in a' < mainfile

Or:

awk '$2 ~ /^(72617|71072|71161)$/' < mainfile

Or:

awk '$2 == "71161" || $2 == "71072" || $2 == "72617"' < mainfile

(Beware that for POSIX awk implementations (not the ones typically found on current Linux distributions though), the == operator applied to strings tests whether the two strings collate the same, which can be different from being equal. For instance on a GNU system in a UTF-8 locale, a POSIX awk would return true for "71161" == "٧١١٦١" because in current versions of those GNU locales, the Eastern Arabic digits collate the same as the Western Arabic (English) equivalents. So to guard against that you may want to set LC_ALL to C).

You can also do numeric comparisons with:

awk '$2 == 71161 || $2 == 71072 || $2 == 72617' < mainfile

Which would also return lines where the second field is 71161.0 or 71.161e3 or 0x115f9 (for GNU awk, you need to pass POSIXLY_CORRECT in the environment for hexadecimal numbers to be considered though).

  • it works perfect. @Stephane. – Batchguy Dec 6 '15 at 23:05
  • @Batchguy If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon Dec 7 '15 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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