Find the answer to your Linux question:
Results 1 to 7 of 7
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    5

    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 OFF
    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"
    FOR /F "TOKENS=2 DELIMS=()" %%A IN ('TYPE TEST.TXT') DO ECHO %%A = "ABS"
    :ENDOFSCRIPT

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

  2. #2
    Just Joined!
    Join Date
    Apr 2005
    Posts
    5
    I should have specified via a shell script..

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    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. #4
    Just Joined!
    Join Date
    Apr 2005
    Posts
    5
    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:192.168.1.1"

    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.

    Thanks!!

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    So the second token would be 192.168.1.1 , right ?
    Well at the end of that pipe you gave you add cut -d: -f2-

  6. #6
    Just Joined!
    Join Date
    Apr 2005
    Posts
    5
    Thank you very much... that was the little utility I've been looking for... thank you very much Helper!

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quicker to use a single command to extract and output:

    Code:
    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
  •