Print #of lines after search string in a big file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print #of lines after search string in a big file
# 1  
Old 07-29-2010
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
Code:
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
Code:
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....

Moderator's Comments:
Mod Comment Having 28 posts you should be familiar using code tags.

Last edited by prash184u; 07-29-2010 at 11:46 AM.. Reason: Changed the title
# 2  
Old 07-29-2010
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.
# 3  
Old 07-29-2010
Hi,

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

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


Reason: Not Eligible
EEEE
FFFF

And your desired output is:
Code:
Reason: Not Active (This is a string1)
AAAA
BBBB
CCCC
DDDD

This command will do it for you:
Code:
awk '/^\s*$/{exit}1' file

HTH Chris
# 4  
Old 07-30-2010
@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 ..
Code:
awk: syntax error near line 1
awk: bailing out near line 1

# 5  
Old 07-31-2010
Code:
# 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 Smilie
# 6  
Old 08-19-2010
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.

Code:
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

Code:
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.
# 7  
Old 08-19-2010
Something like this?
Code:
awk '/^Reason:/{p=10}!NF{p=0}p-->0' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search directory for string and print file name

Good Evening Folks - I have a need to search a specific directory and ALL sub-directories for the following string: Data Load UpdatedIf this string is found, I need to print content from the line directory above it between symbols, as well as the file name where it is found. Here is a... (1 Reply)
Discussion started by: SIMMS7400
1 Replies

2. Shell Programming and Scripting

Script to search every file in a directory and print last few lines

Hi everyone, I need to write a script to search a directory, output the name of a file to an ouput file and print the last few lines of the files to the output file such that I would have something like this: FILE1: LINE LINE LINE FILE2: LINE LINE LINE FILE3: LINE LINE LINE... (2 Replies)
Discussion started by: mojoman
2 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

5. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

6. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

7. Shell Programming and Scripting

search for an expression in a file and print the 3 lines above it

Hi, I have a file like this comment.txt 1.img 2.img 3.img OK x.img y.img z.img not ok 1.img 2.img 3.img bad 1.img 2.img 3.img (7 Replies)
Discussion started by: avatar_007
7 Replies

8. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

9. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies
Login or Register to Ask a Question