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.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Text Manipulation

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 06-07-2006   #1 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 49
Text Manipulation

Hello Guys,

I'm very new to the Linux OS so you'll have to excuse any niave comments i'll make, but i'm sure you'll make me feel welcome.

Basicly i have a command that is presenting me with a reasonable amount of output and i need to grab two parameters from that output and then organise it.

I've been playing around with the GREP, EGREP, TR and CUT commands all day and havn't managed to quite achieve what i'm looking for. Below you will see an example of the output that is generated by my command and below that a rough example of the way i want it to look once it's been trimmed and organised.

Code:
Searching for opush on 00:09:2D:31:69:4C ...
Service Name: OBEX Object Push
Service RecHandle: 0x10008
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 2
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x656e
  encHoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Searching for opush on 00:0E:6D:04:6D:06 ...
Service Name: OBEX Object Push
Service RecHandle: 0x10004
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 9
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100
You can see in bold and red the content that i want to strip out from my output, i should probably say at this point that there is not always two devices listed there could be quite a few more.

I then want to display that content in a list and seperate the address and the channel using a '-' so it resembles somthing like this:

Code:
00:09:2D:31:69:4C-2
00:0E:6D:04:6D:06-9
For now we can just echo that output onto the screen but in future i will be outputing it into a txt file, but i've already got that element sorted.

I'd like to keep this all command line based using things like grep rather than relying on anything like Perl, just as i'm no good with perl and it will make like easier for maintanance.

I've got a whole host of other command line scripts that i will be using in conjunction so i'm reasonably experianced in the code, just this text manipulation has gotten me stumped.

Any advice you can offer would be greatly appreciated.

Thanks,

Rob
SirRawlins is offline  


Reply With Quote
Old 06-07-2006   #2 (permalink)
Trusted Penguin
 
Roxoff's Avatar
 
Join Date: Aug 2005
Location: Nottingham, England
Posts: 2,830
Take a look at the man page for the 'sed' command. It can do string manipulations, and is often used with grep to split lines up. I've never actually used it in earnest (I hardly ever write scripts), but I have seen it used a bit.
__________________
Linux user #126863 - see http://counter.li.org/
Roxoff is offline   Reply With Quote
Old 06-07-2006   #3 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 49
Thanks for that pal.

I've spent a little time looking at the SED command and using it in conjunction with the GREP command.

I'm pretty sure that it can so what i want it to, but its a very complex little bugger with loads of different operations and things.

If anyone uses SED on a regular basis and can offer some advice? I'm quite confident that i'll be able to achieve what i want to with very little code.

At the moment i've worked out this:

Code:
sdptool search opush | egrep 'Searching|Channel' | sed -e "s/    Channel: //" -e "s/Searching for opush on //" -e "s/ .../-/"
That seems to return this:

Code:
00:09:2D:31:69:4C-
2
00:0E:6D:04:6D:06-
9
Which isnt too far from what i want to achieve.

I'm a ColdFusion programmer by nature so all this non tag based stuff has got me totaly confused lol.

Thanks for any more advice guys,

Rob
SirRawlins is offline   Reply With Quote
Old 06-07-2006   #4 (permalink)
Trusted Penguin
 
Roxoff's Avatar
 
Join Date: Aug 2005
Location: Nottingham, England
Posts: 2,830
Quote:
Originally Posted by SirRawlins
I'm pretty sure that it can so what i want it to, but its a very complex little bugger with loads of different operations and things.
Yep - frightens me to death. And there's hardly anything on Linux that does that!
Quote:
Originally Posted by SirRawlins
At the moment i've worked out this:

Code:
sdptool search opush | egrep 'Searching|Channel' | sed -e "s/    Channel: //" -e "s/Searching for opush on //" -e "s/ .../-/"
Hmmmm... It just confirms my fears - that looks complicated. Have you tried google? There may be examples of sed usage on the internet that are pretty close to (or are even exactly) what you're trying to do - i.e. strip a MAC address from a line of text.
__________________
Linux user #126863 - see http://counter.li.org/
Roxoff is offline   Reply With Quote
Old 06-07-2006   #5 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 49
Yeah its frightening alright.

I've found some good resources on google, but as a newbie programmer on this kind of stuff its still very complicated.

I've found that the SED command does have a feature that allows me to connect two lines together, but i've been playing around with the parameter thats meant to do it and cant get anything to work.

I'm sure i'm pretty damn close to the resolution now, just need some programming guru to come on here and help me finish it up lol.

Thanks,

Rob
SirRawlins is offline   Reply With Quote
Old 06-07-2006   #6 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 49
OK, so i've cracked it!

Managed to place this together, which does the task, i supsect its a little sloppy so i would love to hear from any proper programmers on the best way to optomise the code.

Rob

Code:
sdptool search opush | egrep 'Searching|Channel' | sed -e '$!N;s/\n//' -e 's/Searching for opush on //' -e 's/ ...    Channel: /-/'
SirRawlins is offline   Reply With Quote
Old 06-08-2006   #7 (permalink)
Trusted Penguin
 
Roxoff's Avatar
 
Join Date: Aug 2005
Location: Nottingham, England
Posts: 2,830
If it aint broke, dont fix it. You might find you 'optimise out' some of the features that made it work at all...
__________________
Linux user #126863 - see http://counter.li.org/
Roxoff is offline   Reply With Quote
Old 06-09-2006   #8 (permalink)
Linux User
 
muha's Avatar
 
Join Date: Jan 2006
Posts: 290
I was puzzeling as well and almost got it. What's missing is that it should join lines but i could not figure out how to do that ...
Code:
$ sed -n '/Searching for opush on/{s#.*\ \([^ ]*\)\ [^ ]*$#\1#p};/Channel/{s#.*\ \([^ ]*\)$#\1#p};' file
00:09:2D:31:69:4C
2
00:0E:6D:04:6D:06
9
__________________
Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
<~ http://www.linuxcommand.org/ ~>
muha is offline   Reply With Quote
Old 06-09-2006   #9 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 49
Ah interesting attempt,

Does that remove my requirement for the GREP statement?

I've just tried running your code and keep getting

Code:
sed: -e expression #1, char 54: Unknown option to 's''
As an error message when i try and run it.

I have a good SED command that should sort the issue of goining lines to gether though.

Thanks

Rob
SirRawlins 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

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 12:53 AM.






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

Content Relevant URLs by vBSEO 3.3.0 RC2