The text document is organized like a database: strings with time and date.
The format is as follows: 2014-03-25T09:51:34 .

How is it possible to compare this time with the present and if the old date (yesterday and beyond), then change to a new one (tomorrow, the day after tomorrow and beyond). Thank.

  • The task is not entirely clear. If you need to tear out the date from the middle of the line, check and replace it, then I think you should do it on the pearl. And if in the bash script with these dates to work, then it can be done with the command date. Clarify the question, give examples - Mike
  • @Mike Yes, yes, it is this task: choose a date, check and replace. Initially the file is in the java class and the time is recorded as string. I thought it was easier by the script, but what was there that was stuck there. - Josef
  • It is not clear on what basis to replace these dates. You are therefore asked to give an example of a replacement. For example, the first line with the last date is replaced with tomorrow's date, the second line with the last date after tomorrow, etc. Describe in more detail the replacement algorithm that you need to implement. - VenZell

3 answers 3

Script on pearl. Use perl time.pl входной-файл >выходной-файл as perl time.pl входной-файл >выходной-файл

 #!/usr/bin/perl use Date::Parse; use Date::Format; while(<>) { s/(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)/chtime($1)/ge; print; } sub chtime { $time=str2time($_[0]); $now=time(); if($time<($now-24*60*60) ) # проверяем на текущую -24 часа { $time=$now+24*60*60*3; # текущая +3 дня return time2str('%Y-%m-%dT%H:%M:%S',$time); } return $_[0]; } 

The date is pulled from the arbitrary place of the string by a regular expression. The chtime function should check the time and return the new time.

    if the input file represents something like:

     $ cat file.in 2014-03-25T09:51:34 line1 2014-03-25T09:51:34 line2 2016-01-07T01:01:01 line3 

    and the date is older than today, it is necessary to replace it with the current time, then you can use the following script:

     $ cat script date=/bin/date infile=./file.in outfile=./file.out # %s — это секунды с начала unix-эпохи polnoch=$($date -d "00:00" +%s) cat $infile | while read ds; do secs=$($date -d $d +%s) if [[ $polnoch -gt $secs ]]; then d=$($date +%FT%T) fi echo $d $s done > $outfile 

    after its launch:

     $ bash script 

    in the output file we get:

     $ cat file.out 2016-01-07T00:33:59 line1 2016-01-07T00:33:59 line2 2016-01-07T01:01:01 line3 

    if not another time is required, but some other time, then the line should be corrected accordingly:

      d=$($date +%FT%T) 
       #!/usr/bin/env bash i=1 while read d; do DATA=$(egrep -o '201[0-9]-[01][0-9]-[0123][0-9]T[012][0-9]:[0-6][0-9]:[0-6][0-9]' <<<"$d") DATA_T=$(date +%FT%T) [[ -n $DATA ]] && ((($(date +%s) - $(date -d $DATA +%s))/60/60/24)) && sed -i "${i}s/${DATA}/${DATA_T}/" $1 ((i+=1)) done <$1 

      Finds the date in any position of each line and, if necessary, changes it to the current one. Run ./script data.txt