awk: need to extract a line before a pattern

Hello ,

I need your help to extract a line in a big file , and this line is always 11 lines
before a specific pattern . Do you know a way via Awk ?

Thanks in advance

npn35

mate try this,

awk 'BEGIN {}^J{ if( NR == 11 ) print $0 };^JEND{} ' datafile

Cheers,

In fact , I don't want to print the line 11 . I need to search for a specific pattern
then print the line NR-11 .
I hope my explanation is better .

Thanks.
BR

Try this...

/pattern/ && NR == 11 {
print $0;
}

Thanks but the pattern is not located on line 11 . I want to print the line
which is 11 lines before the pattern .

Regards
npn35

hey man u r not so clearabout what u want?cleat itmate.then it would be easier to give u thesolution.

sounds clear to me.

This is few lines of the big file :
....
32 Origine Tr Id : ******* : 0x0dfe5f0d
33
34 CMPT PORTION : Len = 0x1e
35 Component type : 0x0000a1 Invoke
36 Invoke Id : 0x000006 0x06
37 Local Operation: 0x00002d
38 ------------------ fin decodage TCAP ----------------------
39 ------------------ decodage MAP ----------------------------
40 OperationCode : 0x00002d Send Routing Info
41 MSISDN :
42 Extension : 1------- No Extension
43 Nat Of Add : -001---- INTERNATIONAL
44 Num Plan : ----0001 ISDN E164
45 MSISDN : ******** 93799322549
....

I want to get the "Origine Tr Id" line, here the line 32, (Origine Tr Id ...) for a specific MSISDN number (93799322549) (here at the line 45).

Thanks. npn35

don't go by the 'line numbers' - go by the 'patterns':
one pattern being 'Origine Tr Id' - 'cache it' once found.
another pattern is ' MSISDN : ******** 93799322549' - once found output the 'cached' "Origine Tr Id" pattern.

give it a shot.

Try this:

  awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile 

I used line no + 13 as per your file

I created this file by duplicating your records

Running the command,

awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile

gives the output
32 Origine Tr Id : ******* : 0x0dfe5f0d
46 Origine Tr Id : ******* : 0x2efe5f0d

Thank you . It's a very nice solution .
Regards

Dear npn35,

Please use the updated code

Old code: awk '$0~/Origine Tr Id/{lineno=NR} NR==lineno+13 && $0~/MSISDN/{print}' tfile
would print the line containing MSISDN number.

Since you need the Origine Tr Id
use:

awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile

Dear vish_indian,

Thanks again . This is exactly what I need .
I will adapt the command if I need others informations from the data file .

BR , npn35

I want this

I a file with lines containing 3 columns each,

If column 3 of a line is zero, then
substract the second entry of present column from
the second entry of the prvious column.

e.g.

 28      -4.8743      2
 29      -4.8726      2
 30      -4.7833      2
 31      -4.7721      2
 32      -4.7690      2
 33      -4.6266      2
 34      -4.6203      2
 35      -3.8990      1
 36      -3.2009      0
 37      -2.9944      0
 38      -2.9934      0
 39      -2.8856      0
 40      -2.8820      0

then it should subtract -3.2009 from -3.8990 and give me the answer.

Hi friends,

I a file with lines containing 3 columns each,
I want that,

If column 3 of a line is zero, then
substract the second entry of present column from
the second entry of the previous column.

e.g.

 28      -4.8743      2
 29      -4.8726      2
 30      -4.7833      2
 31      -4.7721      2
 32      -4.7690      2
 33      -4.6266      2
 34      -4.6203      2
 35      -3.8990      1
 36      -3.2009      0
 37      -2.9944      0
 38      -2.9934      0
 39      -2.8856      0
 40      -2.8820      0

then it should subtract -3.2009 from -3.8990
and give me the answer.

Thanks!
:slight_smile:

I am assuming the pattern appears in the first field:

$ cat c.sh
#! /bin/sh

if [ $# -ne 2 ]; then
        echo "Usage: $0 <input> <pattern>"
        exit 1
fi


awk '
$1 ~ /^'$2'$/ {
        for (i=1;i<=11;++i) {
                n=getline
                if (n==0) {
                        exit
                }
        }
        print
        exit
}' $1

$ cat c.in
Line1 a line
Line2 a line
Line3 a line
Line4 a line
Line5 a line
Line6 a line
Line7 a line
Line8 a line
Line9 a line
Line10 a line
Line11 a line
Line12 a line
Line13 a line
Line14 a line
Line15 a line

$ ./c.sh c.in Line1
Line12 a line

$ ./c.sh c.in Line4
Line15 a line

$ ./c.sh c.in Line5

$


Oneliner :

awk '{_[NR%2]=$2;l[NR%2]=$NF}$NF==0 && l[(NR+1)%1]!=0 {print _[(NR+1)%1]-$2}' file

.. or script. :rolleyes:

#!/bin/sh
x=0 && y=0;
while read a b c
do
  if test \( $c -gt 0 \) ; then
    x=1 && y="$b";
  elif test \( $c -eq 0 \) -a \( $x -eq 1 \)  -a \( ! -z $y \); then
    echo "$y - $b" | bc && x=0 && y=0;
  fi
done < file