[bash] select column and extract numbers of letters and then take a line.

ref

I think it is powerful enough to upgrade your coding skill.

I used this one for searching specific period of each data of CMIP5 datasets.

 

Result after typing “ls”, ther are serveral data, and I want to know only start and end year.

ts_Amon_CNRM-CM5_rcp85_r1i1p1_200601-205512.nc
ts_Amon_CNRM-CM5_rcp85_r1i1p1_205601-210012.nc
ts_Amon_CNRM-CM5_rcp85_r1i1p1_210101-215012.nc
ts_Amon_CNRM-CM5_rcp85_r1i1p1_215101-220012.nc
ts_Amon_CNRM-CM5_rcp85_r1i1p1_220101-225012.nc
ts_Amon_CNRM-CM5_rcp85_r1i1p1_225101-230012.nc

$ ls *.nc > list                     # save the information to the “list”

$ syear = cut -d’_’ -f6 list | cut -c1-6 | head -1   # take “200601”

$ eyear = cut -d’_’ -f6 list | cut -c8-13 | tail -1    # take “230012”

*cut -d’_’ -f6 list : cut and take only the 6th word after the symbol of ‘_‘ in the “list”

*cut -c1-6 : then, take only 6 letters from the word.

*head -1 : finally, do this at the just one line from the first/last (head/tail).

summary: 200601-205512.nc -> 200601

==========================================================================

So, Simple 🙂