Find in the current line and next line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find in the current line and next line.
# 1  
Old 12-21-2012
Linux Find in the current line and next line.

Hi,

I have lines that have pattern like this.
1)
Code:
productFamilyGroupIndex < Local.ProductFamilyGroup.capacity))

and
2)
Code:
 if (local.getProductFamilyGroup().size() >= Local.ProductFamilyGroup.
        capacity)

So, If I need to find the pattern

grep '\(< \|>= \)Local.*capacity' filename would search only one line.

But, I want it to print 1) and 2) as well. How can I do that?

I have also tried

Code:
pcregrep '(< |>= |> |<= )Local.*(capacity|\n.*capacity)' OscCabCreatePaSaNtwrkDenrm.java

Code:
egrep '(< |>= |> |<= )Local.*(capacity|\n.*capacity)' OscCabCreatePaSaNtwrkDenrm.java


Last edited by Scrutinizer; 12-21-2012 at 04:49 PM.. Reason: extra code tags plus changed icode tags to code tags
# 2  
Old 12-21-2012
if you have gun grep, then you can try below

Code:
grep -A 2 <pattern to be searched>  <file name>

# 3  
Old 12-21-2012
Linux A2 would print only if the pattern matched.

grep -A2 would print only the matching line and the next line

grep '\(< \|>= \)Local.*capacity' filename -A2 will still print 1) and the next line.
But, it will not print 2) because the line 1 on 2) doesn't match.

I am looking for the pattern that matches 1) and 2)
1) and 2) are not subsequent lines. They are present at different locations in the code.

---------- Post updated at 03:52 PM ---------- Previous update was at 03:33 PM ----------

Code:
pcregrep -M '(< |>= |> |<= )Local.*(capacity|.$\n[ ]*capacity)' OscCabCreatePaSaNtwrkDenrm.java

From the man page of pcregrep

In particular, the -M option makes it possible to search for patterns that span line boundaries. What defines a line boundary is controlled by the -N (--newline) option

Last edited by Scrutinizer; 12-21-2012 at 04:51 PM.. Reason: icode tags to code tags
# 4  
Old 12-21-2012
sed concoction:
Code:
sed -n '/Local\..*capacity/{p;d;};/Local\./{N; /Local\..*capacity/p;}' infile

# 5  
Old 12-25-2012
Linux Thanks for sed alternative

I made some modifications to it, but I like the sed alternative. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Copy Column Value Of Next Line Into Current Line

Hello All, I am looking for help to achieve the following: Here is the data set 1757890237|42|55570025|1468796400|0 1757890237|32|55570025|1471474800|0 1757890237|54|55570025|1474153200|1476745200 1757890237|34|55570026|1468796400|0 1757890237|44|55570026|1471474800|0... (7 Replies)
Discussion started by: angshuman
7 Replies

3. Shell Programming and Scripting

Append Next line with current Line bassed on condition

Hi, I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support. my current input data in below format: <Node>xxxxxx <Node>yyyyy</Node> <Node>zzzzzz <Node>12345</node> I need... (9 Replies)
Discussion started by: rramkrishnas
9 Replies

4. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

5. UNIX for Dummies Questions & Answers

Printing the next line side by to the current line

Help, I have a text file which looks like disco 5674536 3456 jambo disco 453678 4578 jambo I would like to have an output which looks like below disco 3456 disco 4578 (4 Replies)
Discussion started by: Indra2011
4 Replies

6. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

7. Shell Programming and Scripting

Search: find current line, then search back

Hello. I want to find a line that has "new = 0" in it, then search back based on field $4 () in the current line, and find the first line that has field $4 and "last fetch" Grep or Awk preferred. Here is what the data looks like: 2013-12-12 12:10:30,117 TRACE last fetch: Thu Dec 12... (7 Replies)
Discussion started by: JimBurns
7 Replies

8. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

9. Shell Programming and Scripting

Vi Editor - How to paste the line concatenated with current line

I generally use yy to copy a line and then p to paste the line at end of current line. But is there a way to paste the copied line in concatenation with the current line with out going to next line. (3 Replies)
Discussion started by: paragkalra
3 Replies

10. Shell Programming and Scripting

awk print the next line on the current line

Ok I have a file with hundreds of lines, four columns, space delimited, TESTB.TXT for example TESTB.TXT --- AA ZZ 12 34 BB YY 56 78 CC XX 91 23 DD VV 45 67 --- I want a new file that has 7 columns, the first four are identical, and the next 3 are the last three of the next line...so... (5 Replies)
Discussion started by: ajp7701
5 Replies
Login or Register to Ask a Question