Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

grep string search for characters on multiple lines

Posted on 2005-04-04
3
Medium Priority
?
975 Views
Last Modified: 2013-12-26
How would grep be used to find a string of continuous characters found on separate lines?  As an example, I would like to find the word "time" in the following text;

abcdefg
hijkl
mnopqr
t
i
m
e
stuvwax
yz
0
Comment
Question by:Shaundinger
3 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 1500 total points
ID: 13701780
I don't think this is an easy thing to do with grep.  The best I can come up with is:

grep -A 3 "^t$" file |egrep "^t$|^i$|^m$|^e$"

Note that this relies on GNU grep.

Also note that it will match

abc
t
m
i
e
xyz

However, if you add tr into the mix, you can do

grep -A3 "^t$" file | tr -d '[\n]' | grep time


You could also use awk/perl to solve this.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13701906
tr -d '\012'<file|grep time
# this wiil do what you requested: find the word time, but it's rather useless, IMHO
0
 
LVL 6

Expert Comment

by:JJSmith
ID: 13730178
The question as posed is that it cannot be done using just grep.
grep matches patterns found within lines, on a line by line basis.

The first solution would fail as it was constructed around the data. ie

abc
ti
me
xyz

would not be matched with ^t$|^i$|^m$|^e$

If your question was "how could I find if a file contains, ( as 4 consecutive characters ) the string time, utilising grep.

Then the second solution of using 'tr' to effectively turn the file into a single line, would allow the use of grep; but then we must ask the question of what we want to achieve and then select the most appropriate tool. Not start with the tool and force it on the problem - we get enough of that in our daily jobs ;-)

Cheers
JJ

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question