Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

printing portion of a line after a pattern match

Posted on 2010-08-16
4
Medium Priority
?
573 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
[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
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 100 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 100 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
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…
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…
Suggested Courses

670 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