How to search between lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search between lines?
# 1  
Old 05-13-2011
How to search between lines?

I have a file that contains data such below

Code:
Start
   line1
   line2
   line3
   line4
End

How can I search for the contents that fall between "Start" and "End"?
# 2  
Old 05-13-2011
Code:
awk '/Start/,/End/ {if ($0 != "Start" && $0 != "End") print}'

# 3  
Old 05-13-2011
Excluding Start and End lines:
Code:
awk '/Start/{getline ;x=1}/End/{x=0}x' tst

# 4  
Old 05-13-2011
Thanks, the commands seem to help, but how do I grep (for instance) line2 between start and end?
# 5  
Old 05-13-2011
Quote:
Originally Posted by bbbngowc
Thanks, the commands seem to help, but how do I grep (for instance) line2 between start and end?
Code:
awk '/Start/,/End/ {if ($0 ~ "line2") print}' file

This User Gave Thanks to shamrock For This Post:
# 6  
Old 05-13-2011
Quote:
Originally Posted by bbbngowc
Thanks, the commands seem to help, but how do I grep (for instance) line2 between start and end?
Code:
awk '/End/{c=x=0}/Start/{x=1;next}x{c++}c==2' tst

Code:
awk '/Start/{x=1;next}/End/{x=0}x++==2' tst

Code:
awk '/Start/{x=NR}NR==x+2' tst


Last edited by ctsgnb; 05-13-2011 at 06:01 PM..
# 7  
Old 05-13-2011
This is like a fancy grep.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for patterns on different lines

im using the following code to search a log for entries on two different lines: awk 'BEGIN{count=0} /'"${firstpattern}"'/,/'"${secondpattern}"'/ { print; if ($0 ~ /'"${thirdpattern}"'/){count++}; } END { print count }' data.txt firstpattern="start error log" secondpattern="i am logging the... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Search and remove the lines

Hallo Team, Hope you are having a wonderful Friday. Here goes i am searching for a pattern and after finding the lines which contain this pattern i want to remove/delete them. This is my code: grep Originating BW*2013*|grep -v "ACCOUNT NOT FOUND"|grep -v "Unknown called number"|grep -v... (2 Replies)
Discussion started by: kekanap
2 Replies

4. Shell Programming and Scripting

search pattern and read lines

Hi, I have a huge de-limited file which has pattern : 99"9876"2010-11-21 12:51:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0 "" <<read>> 99"9876"2010-11-21 12:51:01"K"R-EMP# 01234567 (LOGOFF) "" <<read>> 99"9876"2010-11-21 12:51:01"L" *AUTO LOGOFF* ... (3 Replies)
Discussion started by: angie1234
3 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. UNIX for Dummies Questions & Answers

Printing the lines using search patterns

Hi all , i need an help here.!!!! i have a file that contains /etc/passwd files from some servers. i need a script which search for presence of a user in the servers. like if i give 51144 to the script. the should be o/p Please help on this..... (4 Replies)
Discussion started by: sudharson
4 Replies

7. UNIX for Dummies Questions & Answers

Search for lines in one file in another

I am trying to search for lines present in file a that do not exist in file b and print out the lines. E.g. file a apple pear banana orange file b apple banana orange would output to stdout: pear. ... (3 Replies)
Discussion started by: thurmc
3 Replies

8. Shell Programming and Scripting

Perl to Search through next lines

I have log file that I need to extract time difference occurance when two events happend, between first occurance of TV and when W_NO happend. also read the value=, below example...I can only read the next line but not able to seach all the next lines untill i see W_NO.. Thanks for your help.... (6 Replies)
Discussion started by: bataf
6 Replies

9. UNIX for Dummies Questions & Answers

Search for different lines in 2 files

Hi guys I need help in comparing 2 files for example file1: asdf|gdddd|acx asdf|vcxzz|eww avxc|bvvce|qwe file2: qwer|asdfg|sss avxc|bvvce|eww aass|weews|llk I need to get the lines that are present in file1 but not found in file2. so in these case (5 Replies)
Discussion started by: khestoi
5 Replies

10. Shell Programming and Scripting

search for lines in a file

Hello I need to check if following three files exist in a file, how to do that in shell script: 1. ALL MACHING RECORD COLUMNS MATCHED (Baseline and Regression File) 2. Total Mismatched Records (Baseline File): 0 3. Total Mismatched Records (Regression File): 0 Currently I am seaching only... (9 Replies)
Discussion started by: shalua
9 Replies
Login or Register to Ask a Question