Manipulate Data with Date command

Asked

Viewed 1,690 times

2

Expensive;

I have a very simple problem, but I’m not finding a logic for it. I have a file with pre-fixed dates, example 2017-10-31, I would like each month change, this date be added with the current month. On the date above is month 10 (October), in November it was changed to 11 and so on. This change could be with a for + 1, but when arriving on 12, would return to 01, however , the year would continue 2017 and not 2018. I believe that 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
  • makes an example if if(mes == 12 window.mes != 'newAno'){ window.mes == 'newAno' }Else{ year + 1; window.mes =="" } ? creates a global variable window.mes and then when the month is twelve and adding a new month it will go to the I ai vc sum the year+1

  • 1

    Thank you Marcos, I resolved as edited in my question

2 answers

1

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

date +%Y-%M-%d

will have the following output

2017-04-01

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, 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"

exit

Estamos no ano de 2017 dia 01 mes 11

Read more about the date command in --> https://linux.die.net/man/1/date

0

The ideal would be to put some lines of the file for better reference, but based on your command line I believe this resolves.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.