0

I was planning on solving the first question of aoc2022, using awk, inspired by someone's idea from GitHub. I need to find the sum of elements between the blank spaces.

Here's the awk program,

#!/usr/bin/awk -f

BEGIN { 
current_count = 0 ; 
}
 
NF == 0 {   
print current_count ;   
current_count = 0 ;   
next ; 
}

{ current_count += $0 }

But when I run this, I'm not getting any outputs.

 awk -f calorie-count.awk < input.txt

Any help is appreciated.

The input almost looks like this, with white spaces in between.

7569
1357
10134
4696
4423
8869
3562
6597

4038
9038
1352
8005
4811
6281
3961
4023

7234
3510
7728
1569
4583
7495
3941
6015
6531
2637

18211
19347
16113
7314

3359
7199
4527
5384
1752
2422
6301
1923
5957
3000
6835
1986
10
  • 1
    what @ilkkachu said but at the end you need END { print current_count } instead. Dec 30, 2022 at 9:00
  • 1
    you might also have a Windows like end-of-line (CR-LF, Carriage Return-Line Feed) input. First do dos2unix input.txt on then try again. Dec 30, 2022 at 9:19
  • 1
    Thanks @αғsнιη it worked. I was on WSL and never knew this would've been the issue. Thanks man!!
    – albus
    Dec 30, 2022 at 9:33
  • 1
    you are welcome! That's a common issue when working on Windows like/generated input on *nix boxes. you will definitely need to add END { print current_count } after your first fix (changing CRLF to LF which dos2unix commnad does) Dec 30, 2022 at 9:39
  • 1
    @αғsнιη thanks again
    – albus
    Dec 30, 2022 at 10:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.