?
Solved

grep string search for characters on multiple lines

Posted on 2005-04-04
3
Medium Priority
?
965 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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