Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=10 s="STRING1" FILE

The file is 5 gig big.

It works great and prints 10 lines after the lines which contains search string in the file....

I want to print the 10 lines only if I don't have blank lines in between. I want to print lines until it reaches blank line.

Example .. Currently I get the out put

Reason: Not Active (This is a string1)
AAAA
BBBB
CCCC
DDDD


Reason: Not Eligible
EEEE
FFFF

I do not want last 3 lines. I want to break the output if it finds the blank line

Thanks in advance gurus....

i am a beginner in unix,

i think we could pipe the output you got from above script and extract only the top portion you needed.

Hi,

i don't understand your awk-command, but suppose your file is:

Reason: Not Active (This is a string1)
AAAA
BBBB
CCCC
DDDD


Reason: Not Eligible
EEEE
FFFF

And your desired output is:

Reason: Not Active (This is a string1)
AAAA
BBBB
CCCC
DDDD

This command will do it for you:

awk '/^\s*$/{exit}1' file 

HTH Chris

@Phoenix :

I may get above output number of times in the result.
I just wanted to break the result lines if there is a blank line and go for the next set.

@Chris ...

Thanks Chris but the command is giving me an error ...
I use Solaris ..

awk: syntax error near line 1
awk: bailing out near line 1
# sed -n '{N;H;N;H;N;H;N;H;N;H;N;H;N;H;N;H;N;H};s/\n\n.*$//;p'  infile

delete blank lines and after lines for every 10 lines :wink:

C'mon Gurus...

All I want is to look for the pattern in the file...If I found it at 5 places... all I want to do is print lines after those pattern(line) until I fond a blank line.


Log EXAMPLE :


MT:Exception caught 
The following Numbers were affected:
1234
2345
2346

Error
java.lang.InternalError: Can't connect to X11 
      Exception caught 

The following numbers were affected:
11111
22222
33333
44444
55555

java.lang.InternalError: Can't connect to 

This is the command I used

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=10 s="The following numbers were affected:" Process.log 

Above command gives me 10 lines after the serach string.

But I just want the 3 and 5 lines in this case. I want to break the print if it hits the blank line.

Something like this?

awk '/^Reason:/{p=10}!NF{p=0}p-->0' file
$
$
$ cat -n f1
     1  MT:Exception caught
     2  The following Numbers were affected:
     3  1234
     4  2345
     5  2346
     6
     7  Error
     8  java.lang.InternalError: Can't connect to X11
     9        Exception caught
    10
    11  The following numbers were affected:
    12  11111
    13  22222
    14  33333
    15  44444
    16  55555
    17
    18  java.lang.InternalError: Can't connect to
$
$
$ # If you want to print just the affected numbers, then -
$
$ perl -lne 'if (/^The following numbers were affected:/i){$x=1} elsif(/^\s*$/){$x=0} elsif($x) {print}' f1
1234
2345
2346
11111
22222
33333
44444
55555
$
$
$ # If you want to print the message as well as the affected numbers, then -
$
$ perl -lne 'if (/^The following numbers were affected:/i){print; $x=1} elsif(/^\s*$/){$x=0} elsif($x) {print}' f1
The following Numbers were affected:
1234
2345
2346
The following numbers were affected:
11111
22222
33333
44444
55555
$
$

tyler_durden

1 Like

Thank you so much Durden_Tyler... GR8 help....