Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, When I use getline var the next line is stored in var and it is actually making the awk to jump to the next record. But getline var should ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2

    Unhappy AWK program "getline" help - pls !!!!!

    Hi,

    When I use getline var the next line is stored in var and it is actually making the awk to jump to the next record.

    But getline var should not make awk to jump to next record. rather it should make awk stay in the same record.

    Example:

    AWK SCRIPT:
    -------------
    awk -F '-' '{
    tmpLine = "EMPTY"
    print "CURRENT LINE :"$0
    getline tmpLine
    print "NEXT LINE :"tmpLine
    }' testawk.txt

    testawk.txt
    --------------
    1-TEST-FILE-NEW
    2-TEST-FILE-NEW
    3-TEST-FILE-NEW
    4-TEST-FILE-NEW
    5-TEST-FILE-NEW
    6-TEST-FILE-NEW
    7-TEST-FILE-NEW

    output:
    -------
    CURRENT LINE :1-TEST-FILE-NEW
    NEXT LINE :2-TEST-FILE-NEW
    CURRENT LINE :3-TEST-FILE-NEW
    NEXT LINE :4-TEST-FILE-NEW
    CURRENT LINE :5-TEST-FILE-NEW
    NEXT LINE :6-TEST-FILE-NEW
    CURRENT LINE :7-TEST-FILE-NEW
    NEXT LINE : EMPTY

    desired output
    --------------
    CURRENT LINE :1-TEST-FILE-NEW
    NEXT LINE :2-TEST-FILE-NEW
    CURRENT LINE :2-TEST-FILE-NEW
    NEXT LINE :3-TEST-FILE-NEW
    CURRENT LINE :3-TEST-FILE-NEW
    NEXT LINE :4-TEST-FILE-NEW
    CURRENT LINE :4-TEST-FILE-NEW
    NEXT LINE :5-TEST-FILE-NEW
    CURRENT LINE :5-TEST-FILE-NEW
    NEXT LINE :6-TEST-FILE-NEW
    CURRENT LINE :6-TEST-FILE-NEW
    NEXT LINE :7-TEST-FILE-NEW
    CURRENT LINE :7-TEST-FILE-NEW
    NEXT LINE : EMPTY

    Kindly help me in sorting out the mistake.
    Thanks in advance.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    3.8.2 Using getline into a Variable

    You can use `getline var' to read the next record from awk's input into the variable var. No other processing is done. For example, suppose the next line is a comment or a special string, and you want to read it without triggering any rules. This form of getline allows you to read that line and store it in a variable so that the main read-a-line-and-check-each-rule loop of awk never sees it.

    -- excerpt from Getline/Variable - The GNU Awk User's Guide
    So this suggests that your code is operating according to the defined operation of getline (and is how I have understood that it works).

    What documentation do you have that says otherwise? ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2

    Thank U !!

    Hi Drl,

    "You can use `getline var' to read the next record from awk's input into the variable var. No other processing is done"

    Reading the statement "No other processing is done", I thought that this can be used to read the next line in queue ( by not removing it)

    Sorry its my fault.

    And is there any possibilty to get the desired ouput with awk,,

    Thank u..

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    In many tools a "push-back" mechanism is commonly used when you need to look ahead, be it a character, a line, etc. The reason is that sometimes when you read ahead, you see that really didn't need to, so you push-back the item you just read. That makes the item available for the next read.

    I don't think such a mechanism is a standard awk function, so you will probably need to create your own. I have not done it in awk because awk does the file input for the main loop automatically. In other languages, you would have an area of storage, and your function would maintain that area. You would have at least 2 methods -- one to read a line, and one to push-back a line. In the function, if there is a pushed-back line, you would deliver that, otherwise you read from the file. In place of the getline you were using, you would read and immediately push-back, so that the main loop can read as usual -- except of course, the main loop does not know about your input function, so you would also need to ensure that the main is never executed more than once.

    It's an interesting problem, but I'd encourage you to think of other solutions. Doing the code in a different language, say python or perl, might make it easier.

    Other posters might provide some advice here as well.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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