3

I have a text file. This file includes characters and numbers as follows:

ANKR00TUR_R_20183240000_01D_30S_MO.rnx:  2018    11    20    00    00    0.0000000     GPS         TIME OF FIRST OBS
brmu3350.14o:  2014    12     1     0     0    0.0000000     GPS         TIME OF FIRST OBS
KNY12040.14o:  2014     7    23     0     0    0.0000000     GPS         TIME OF FIRST OBS
rinex_quantity:grep "TIME OF FIRST OBS" * > time_of_first_epochs

I need to extract only 4 digits numbers and store them into another file as follows:

2018
2014
2014

I applied the following code but it extracts all 4 digit numbers:

grep  -Po "\d{4}" data

2018
3240
2018
0000
3350
2014
0000
1204
2014
0000
  • you need to extract the digit number after the colon? – AtomiX84 Jan 14 at 10:17
7

Your grep command was almost correct, you just have to anchor the pattern to match only if there is a word boundary before or after it.

Word boundaries are zero-length patterns that match between a word-character (letters, digits, underscore) and a non-word charater (e.g. spaces, other punctuation, line end, and everything else).

In grep, you can either do this by surrounding your pattern with \b, or by using the -w switch to enable word matching:

$ grep -Po '\b\d{4}\b' data
2018
2014
2014

$ grep -Pow '\d{4}' data
2018
2014
2014
0


with miller (http://johnkerl.org/miller/doc) is

mlr --implicit-csv-header --pprint  cut -f 2 then label year input

As output you will have

year
2014
2014

Mi input is

brmu3350.14o:  2014    12     1     0     0    0.0000000     GPS         TIME OF FIRST OBS
KNY12040.14o:  2014     7    23     0     0    0.0000000     GPS         TIME OF FIRST OBS

I have simply extracted the second column with cut

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.