Solved

Grep string from multiline

Posted on 2003-12-10
1
1,026 Views
Last Modified: 2013-12-26
Hi,

I need to use grep to gerp out a string from 2 lines. The file like:
ABCDEF123456
890YEAHYEAHD
ABCDEFA23AAA
890YEAHYEAHD
ABCDEF654321
890YEAHYEAHD

The result should be:
ABCDEF123456890YEAHYEAHD
ABCDEF654321890YEAHYEAHD
(The 2 lines with 8 continuous digit.)

How can I do that?
0
Comment
Question by:matchz
1 Comment
 
LVL 1

Accepted Solution

by:
mbekker earned 50 total points
ID: 9919865
Hi matchz,

The following works for above example:

sed 's/[^0-9]$/&#/' filename | \
 tr '#\n' '\n#' | \
 sed 's/#//g' | \
 grep "[^0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9]"

Only this one greps 9-digit numbers, in your example you have nine digits. If you want for example at least 8 digits the [^0-9] parts before and after could be removed.

It's not a neat solution, but it works. And as far as I know there is no way to grep out of two lines with the grep command only.

Good luck!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now