Question: I want to use awk for printing a match of some pattern, only if it is the last line of the file before some constant text.

For example, i want to print the value of "c" only if it is the last line before some constant text:

```
cat file.txt:
a=1
b=2
c=3
...constant text...

```

print 3.

```
cat file.txt:
a=1
c=3
b=2
...constant text...

```

do not print anything.

How do I do that..? How can awk know if it's the last number of the file?

Answer: 
> 
> I want to use awk for printing a match of some pattern, only if it is
>  the last line of the file before some constant text.
> 
> 
> 

In that case, try this:

```
awk -F= '/constant text/ && name=="c"{print value} {name=$1;value=$2}' file

```

### Examples

```
$ cat file1 
a=1
b=2
c=3
...constant text...
$ awk -F= '/constant text/ && name=="c"{print value} {name=$1;value=$2}' file1
3
$ cat file2
a=1
c=3
b=2
...constant text...
$ awk -F= '/constant text/ && name=="c"{print value} {name=$1;value=$2}' file2
$ 

```

### How it works

* `-F=`

This tells `awk` to use `=` as the field separator. In this way, we can separate the name and value for fields like `a=1` or `c=3`.
* `/constant text/ && name=="c"{print value}`

If we reach a line with `constant text` and `name` (as last set on the previous line) is `c`, then print `value`.
* `name=$1;value=$2`

For the current line, save the first field as `name` and the second field as `value`.
If you don't mind using grep it has an option to print lines before match.

Option is : -B num, --before-context=num

In your case :

```
cat file.txt|grep -B 1 "constant text"|grep "c="|cut -f2 -d'='

```

Explanation :

1. Print just the line before constant text.
2. Filter all c values from output.
3. Extract second field with '=' delimiter.