Find the answer to your Linux question:
Results 1 to 9 of 9
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 ...
  1. #1
    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

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,318
    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://linuxcounter.net/

  3. #3
    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

  4. #4
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,318
    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://linuxcounter.net/

  5. #5
    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

  6. #6
    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: /-/'

  7. #7
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,318
    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://linuxcounter.net/

  8. #8
    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/ ~>

  9. #9
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •