Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > The Community > The Coffee Lounge
Reload this Page Need to extract a string from unstructured file.
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

The Coffee Lounge General chat about anything that goes, a good place to introduce yourself and say hi, tell a Joke, or just relax.

Site Navigation
Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 03-21-2008   #1 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 0
Need to extract a string from unstructured file.

First, I apologize if I've reached the wrong forum for this scripting related question but a forum search didn't find me any useful hits so I came here.

What I'm trying to accomplish is to be able to locate and extract a copy of a string from an unstructured text file. I can use various commands combined with a regexp to location virtually anything I want but it returns whole lines. The only tools I'm familiar with require columnized data with known delimiters so I'm stuck.

So for example:
# egrep '..:..:..:..:..:..' dmesg
e100: eth0: e100_probe: addr 0xc0110000, irq 20, MAC addr 00:0D:60:07:92:22
#

Now I need to grab only '00:0D:60:07:92:22' from the returned line of text without knowing the delimiter or position of what I'm looking for. If anybody knows a technique for doing this without having to loop through the data, it'd really be helpful.

Any ideas are welcome--thanks.
t.o.m is offline   Reply With Quote
Old 03-22-2008   #2 (permalink)
Linux Enthusiast
 
Join Date: Apr 2004
Location: UK
Posts: 583
I'd imagine there is a more elegant solution than this, but it works for your example.

Code:
chris@angua:~/dev/scratch$ cat test.txt
e100: eth0: e100_probe: addr 0xc0110000, irq 20, MAC addr 00:0D:60:07:92:22
chris@angua:~/dev/scratch$ sed -n 's/^\(.*\)\(..:..:..:..:..:..\)\(.*\)$/\2/p' test.txt
00:0D:60:07:92:22
Let us know how you get on.

Chris...
__________________
DRM keeps an honest user loyal
kakariko81280 is offline   Reply With Quote
Old 03-22-2008   #3 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 0
Perfect--thanks. I was hoping to do this on one line. Now to beef up my grasp of reg expressions so I can figure out what's going on with your sed command.
t.o.m is offline   Reply With Quote
Old 03-22-2008   #4 (permalink)
Linux Enthusiast
 
Join Date: Apr 2004
Location: UK
Posts: 583
The search is breaking the line into three parts.

^ anchors the search to the start of the line.
(.*) will match any amount of anything
(..:..:..:..:..:..) matches the mac address.
(.*) matches any amount of anything
$ anchors the search to the end of the line

Putting things in brackets groups them so they can be referred to later \1, \2 and \3. We are only interested in \2. The brackets have to be escaped with a \ to tell sed that we are grouping things rather than looking for brackets in the text.

Finally use the -n switch to prevent sed outputting everything along with the 'p' flag on the search command to print the line we are interested in.

I'm glad it worked. I've only recently started using regular expressions myself. awk has saved me weeks of effort already and I'm looking into learning perl.

Chris...
__________________
DRM keeps an honest user loyal
kakariko81280 is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Job Search
keyword location
Post a Job »
job title, keywords or company
city, state or zip jobs by job search

Free Magazines
Free eBook:"Vulnerability Management for Dummies"
Get all the Facts and See How to Implement a Successful Vulnerability Management Program.
subscribe
Google vs The World: The Battle of the Message Security Vendors
With such a powerful name behind it, Google Message Security stands out in a sea of products that do exactly the same thing - or so they say. So when it comes right down to it, how does the Google selection stack up against the rest of messaging security's big guns?
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 12:48 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0