Solved

printing portion of a line after a pattern match

Posted on 2010-08-16
4
567 Views
Last Modified: 2012-05-10
Hi, I have a number of lines which I need to extract only the portion of the line after a pattern match is found

e.g.

2010-08-16 11:34:57,869 text: text: ....text ID: rest of line

so I want to only show "rest of line" based on it finding a match for "ID:"
(note there coud be a different number of ":" preceeding the pattern to match "ID:")

This is to be used in a Solaris 10 shell script and probably using something like awk, but can't find out how to do this

Thanks
Mark
0
Comment
Question by:mgferg
4 Comments
 
LVL 2

Expert Comment

by:gosvald
ID: 33445432
You can use grep command for this. It should be available on Solaris. You can specify how many lines you want to see as well. If you do not use any extra options it will just give you the line where it found the pattern.
The syntax is:

grep [options] PATTERN [FILE...]
or
grep [options] [-e PATTERN | -f FILE] [FILE...]

So  in you case:

grep -R ID *

this will search for ID in all files. If you want to find out more just read the grep man file.
 
0
 
LVL 8

Accepted Solution

by:
mustaccio earned 25 total points
ID: 33446822
If you are OK with using perl, here's one way to do it:

perl -pn -e 's/^.*ID:(.*)$/$1/' yourinputfile
0
 

Author Comment

by:mgferg
ID: 33447590
-R seems to be an invalid option

and note I don't want the full line - I'm using grep to get that already - I only want portion of the line "after" a "pattern match"
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 33448048
If not all lines contain a match for ID: you could use
perl -lne 'print $1 if /ID:(.*)/' yourinputfile
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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

13 Experts available now in Live!

Get 1:1 Help Now