Solved

printing portion of a line after a pattern match

Posted on 2010-08-16
4
570 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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