Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Parsing certain words from a line

    Curious of the methods in which I could parse a line of text and store certain series of char's in an array or similar w/ the ability to specify a delimiter. I use the "for" command on my M$ boxes, but am looking to do the same on the others.

    M$ Example (contents of test.cmd): [[[[NT Kerneled only]]]]

    ECHO This is a test of the American Broadcast System (ABS)... > Test.txt
    FOR /F %%A IN ('TYPE TEST.TXT')DO ECHO %%A = "This"
    FOR /F "TOKENS=1,2" %%A IN ('TYPE TEST.TXT') DO ECHO %%A = "This" and %%B = "is"

    I don't think I'm describing what I'm looking for all that well, hence the M$ example. Thanks all!!

  2. #2
    I should have specified via a shell script..

  3. #3
    Your example doesn't help I'm afraid because I'm not familiar with
    the language.(Out of curiosity , which language is it ?)

    On Linux the first thing that comes to mind is use awk and have the
    shell script read its output but I'm sure there's a more straightforward way.

  4. $spacer_open
  5. #4
    sorry, it was pretty late when I posted.

    The example is a microsoft batch file using the "for" command.

    The 1st goal would be to write a script that returns "only" the ip address for whatever interface is specified. Here is as far as I got..

    ifconfig eth2 | grep "inet addr" | awk '{ print $2 }'
    will return: "addr:"

    So on a M$ box, I'd do a
    FOR /F "TOKENS=2 DELIMS=:" %%A IN ('%VAR%') DO ECHO IP = %%A

    so that I set a delimiter of a ":" and put the second "token" into the variable %%A.


  6. #5
    So the second token would be , right ?
    Well at the end of that pipe you gave you add cut -d: -f2-

  7. #6
    Thank you very much... that was the little utility I've been looking for... thank you very much Helper!

  8. #7
    Quicker to use a single command to extract and output:

    ifconfig eth2 | sed -n 's/.*inet addr:\([0-9.][0-9.]*\).*/\1/p'
    sed's -n suppresses output of non-matching lines. We're matching everything up to "inet addr:" saving the IP address and discarding the rest of the line. We replace the whole line with the saved portion and print the line.

Posting Permissions

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