• Uncategorized

About linux : How-to-add-a-number-and-rearrange-a-date-in-linux

Question Detail

When I do this:

 a=cut -c 25-31 <<< 'REFDV.UJH.c01r00.GLOBAL.2000.06.99.9999.MLM.10KM.ZZ00.MM1.00.nc'

I got echo a

 2000.06

I would like to systematically return a as

   01062000 

always add 01 and arrange it differently

Question Answer

A more efficient approach might be to obtain and format the output in one operation using awk, i.e.

$ awk -F'.' '{ print "01"$6$5 }' <<< 'REFDV.UJH.c01r00.GLOBAL.2000.06.99.9999.MLM.10KM.ZZ00.MM1.00.nc'
01062000
$

If you want to save the output to a shell variable named a:

$ a=$(awk -F'.' '{ print "01"$6$5 }' <<< 'REFDV.UJH.c01r00.GLOBAL.2000.06.99.9999.MLM.10KM.ZZ00.MM1.00.nc')
$ echo "$a"
01062000
$

$ a='REFDV.UJH.c01r00.GLOBAL.2000.06.99.9999.MLM.10KM.ZZ00.MM1.00.nc'

$ d=$(echo $a | cut -d . -f 5-6 --output-delimiter=- | sed 's/\(.*\)/\1-01/')
$ echo $d
2000-06-01

$ date -d "$d" +%d%m%Y
01062000

$ date -d "$d" +%c
Thu 01 Jun 2000 12:00:00 AM CDT

You can try


year=`echo $a | cut -d '.' -f1 `
month=`echo $a | cut -d '.' -f2 `
echo "01$month$year"

nabil@LAPTOP:~$ echo "01$month$year"
01062000

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.