Handle Date with Date command

2

Expensive;

I have a very simple problem, but I can not find a logic for this. I have a file with fixed dates, example 2017-10-31, I would like every month change, this date would be added with the current month. At the top date is month 10 (October), in November it was changed to 11 and so on. This change could be with a for + 1, but when it reaches 12, it would return to 01, however, the year would continue 2017 and not 2018. I believe the date command would have some argument for this and not use some script.

I tried to use:

date +2016-10-26 +%m

date "+2016-10-26" --date="1 month"

Solved with the code (Gambiarra) below

ano="$(date +%Y)"

dia='cat mensal.txt | cut -d: -f3| sed s'/-/ -/g'| awk '{print $3}'| sed 's/-//g''

DATA_ALVO="$(date +%m)"

echo $ano-$DATA_ALVO-$dia
    
asked by anonymous 26.10.2017 / 23:01

2 answers

1

I imagine that you want to print format the date before printing, the correct command is:

date +%Y-%M-%d

will have the following output

2017-04-01

You want to declare a variable with the current date ...

var=$(date +%Y-%m-%d)

Printing the variable

echo $var
2017-04-01

You can format the date in many ways, for example:

#!/bin/bash
var_ano=$(date +%Y)
var_mes=$(date +%m)
var_dia=$(date +%d)

echo "Estamos no ano de $var_ano dia $var_dia mes $var_mes"

output

Estamos no ano de 2017 dia 01 mes 11

Read more about the date command in - > link

    
01.11.2017 / 20:16
0

The ideal would be to put a few lines of the file for better reference, but based on your command line, I think this will solve.

sed -i -r "s/:([^-]+-)[^-]+/:$(date +%m)/" arquivo
    
27.10.2017 / 03:03