Script to find string and return next 2 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find string and return next 2 lines
# 8  
Old 10-09-2008
Hammer & Screwdriver A strange appraoch; but appears to give desired results

Although my main command seems to do a lot of substitution, it does provide the next two lines of text after the line containing "query"

Code:
> cat file4
msj
mh
query
return this 1
return this 2
mjk
mhj
query
return this 3
return this 4

> sed "s/^query/~query/" file4 | tr "\n" "#" | tr "~" "\n" | grep "^query" | cut -d"#" -f2-3 | tr "#" "\n"
return this 1
return this 2
return this 3
return this 4
>

# 9  
Old 10-09-2008
It seems like grep should be able to do this. If we call your datafile "data.file", you should be able to do something like:

Code:
grep -A 2 query data.file

If you want to exclude the actual matching line, just do:

Code:
grep -A 2 query data.file | grep -v query

I haven't tested it, but it seems it should do just what you need.
# 10  
Old 10-09-2008
Code:
awk '/query/{getline; print; getline; print}' filename

# 11  
Old 10-09-2008
Quote:
Originally Posted by daveaasmith
...
I want the lines after (and including) "Statement", up to (not including) "Stack Trace". There may not always be the same number of lines between the two.

(Example File)
Statement Execution Time
Select Distinct.......
From.........
Where..........
Order By........
Stack Trace
config.java.feeds.invoices

Or you can use flags:


Code:
awk '/Statement Execution Time/{f=1}/Stack Trace/{f=0}f' file


Output:

Code:
Statement Execution Time
Select Distinct.......
From.........
Where..........
Order By........

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

Find a string and then return the next 20 characters in multiple files

Hello all, I have a directory with 2000+ files. I need to look in each file for an invoice number. To identify this, i can search for the string 'BIG' and then retrieve the next 30 characters. I was thinking awk for this, but not sure how to do it. Each file contains one long string and in... (8 Replies)
Discussion started by: jdinero
8 Replies

4. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

7. Shell Programming and Scripting

Help to find string and return following characters from list of files

Hi, I'm fairly new to UNIX, but hopefully some-one can help me with this: I am using the following code to find files with the name "example.xml": find . -name "example.xml" -print that would print me a list like the example here: ./dir1/dir2/example.xml... (5 Replies)
Discussion started by: boijie
5 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies

10. Shell Programming and Scripting

find string, then get the next 3 lines in a file

Hi all. HPUX - /bin/sh (posix) I am parsing a 3 field flat file, space deliminted example data.file acct dining mem open 0 50 dep 50 0 close 255 0 acct plus mem open 100 100 dep 50 0 close 323 0 I would like to find strings, then write the next 3 lines out to another file ... (8 Replies)
Discussion started by: lyoncc
8 Replies
Login or Register to Ask a Question