I have a question about awk statement.

Asked 2 years ago, Updated 2 years ago, 50 views

Example

July 1st, July 2nd, July 3rd...September 30th
10000   10000 20000       13000

Assume you have a file similar to the one above.

How do I add them up from July 1st to July 3rd and repeat them semi-permanently as many numbers as possible?

July 1st, July 2nd, July 3rd 
10000   10000 20000   
↓
July 1-3 July 2-4 July 3-3 July 3rd
40000   ・・・  ・・・

It looks like this.

linux

2022-09-30 21:30

1 Answers

Since we do not know the delimiter characters in the columns of input data, we assume that the output is tab-delimited, assuming a sequence of spaces and tab characters.

awk-F'[\t]+|month|day[\t]*'-vOFS='\t''
  NR == 1 {
    idx = 0
    for(i=1;i<=(NF-5);i+=2){
      idx++
      if($i==$(i+4)){
        $idx=$i "month"$(i+1)"~"$(i+5)"day"
      } else{
        $idx=$i "month"$(i+1)"day to"$(i+4)"month"$(i+5)"day"
      }
    }
    NF = idx; print
  }
  NR == 2 {
    for(i=1;i<=(NF-2);i++){
      $i = $i+$(i+1)+$(i+2)
    }
    NF = idx; print
  }
' data.txt

If the month changes, print the entire start and end dates.

...July 29-31 July 30th -August 1st July 31st -August 2nd August 1st -August 3rd...


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.