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
# 8  
Old 08-19-2010
Quote:
Originally Posted by prash184u
...But I just want the 3 and 5 lines in this case. I want to break the print if it hits the blank line.
Code:
$
$
$ 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
This User Gave Thanks to durden_tyler For This Post:
# 9  
Old 08-19-2010
Thank you so much Durden_Tyler... GR8 help....
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