Solved

Grep string from multiline

Posted on 2003-12-10
1
1,027 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fizzArray2 challenge 1 72
matchUp  challenge 9 94
mapAB Challlenge 35 126
Capture logon name 13 42
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

15 Experts available now in Live!

Get 1:1 Help Now