Question: I'm trying to concatenate String values and print them, but if the last types are Strings and there is no change of type then the concatenation won't print:

input.txt:

```
String 1
String 2
Number 5
Number 2
String 3
String 3

```

awk:

```
awk '

BEGIN { tot=0; ant_t=""; }

{ 
  t = $1; val=$2;
  #if string, concatenate its value
  if (t == "String") {
    tot+=val;
    nx=1;
  } else {
    nx=0; 
  }
  #if type change, add tot to res
  if (t != "String" && ant_t == "String") {
    res=res tot;
    tot=0;
  }
  ant_t=t;
  #if string, go next
  if (nx == 1) {
    next;
  }
  res=res"\n"val;
}

END { print res; }' input.txt

```

Current output:

```
3
5
2

```

Expected output:

```
3
5
2
6

```

How can I detect if awk is reading last line, so if there won't be change of type it will check if it is the last line?

Answer: 
`awk` reads line by line hence it cannot determine if it is reading the last line or not. The `END` block can be useful to perform actions once the end of file has reached.

To perform what you expect

```
awk '/String/{sum+=$2} /Number/{if(sum) print sum; sum=0; print $2} END{if(sum) print sum}'

```

will produce output as

```
3
5
2
6

```

what it does?

`/String/` selects line that matches `String` so is `Number`

`sum+=$2` performs the concatanation with `String` lines. When `Number` occurs, print the `sum` and reset to zero
Like this maybe:

```
awk -v lines="$(wc -l < /etc/hosts)" 'NR==lines{print "LAST"};1' /etc/hosts

```

I am pre-calculating the number of lines (using `wc`) and passing that into `awk` as a variable called `lines`, if that is unclear.