Results 1 to 6 of 6

Thread: Affected shell variable in awk

  1. #1
    Join Date
    Oct 2009
    Posts
    67

    Affected shell variable in awk

    Hi
    I try to assign a variable in awk previously defined in my script, but I can not find the correct syntax:
    Code:
    / bin / bash
    var = 2
    
    awk-F '' '
        BEGIN (
            tata = $ 1
            $ var = tata
        )
        END '
    
    # # Here, the script continues, and var is inside tata.
    Could you help me with line number 7?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Affected shell variable in awk

    Hi
    I think your calls are not clear:
    Code:
    varBash = 2
    awk-F ''-v varAwk = $ varBash '(uneVar = $ 1; varBash = uneVar; print uneVar, varBash, varAwk)' <<< "this is an example"
    Think you should correct this and I suggest you to keep your code clean so that it can to readable to you in future.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Affected shell variable in awk

    Hi
    Note that the $ are used for variables "columns" ($ 0 is the complete line, $ 1, $ 2 ... being the columns) so when you do $ var = tata, it's like if you replaced it by var = $ 0.
    You have to do is simply
    Code:
    var = tata to affect the contents of the variable in the variable tata 
    and do var = "tata" to assign the string variable in tata var
    Hope this helps you. If nay more queries then do post back.

  4. #4
    Join Date
    Oct 2009
    Posts
    67

    Re: Affected shell variable in awk

    Hello,
    I said my application in simplifying the example:
    Code:
    / bin / bash
    var = 2
    awk-F '' '
        BEGIN (
            var = 3
        )
        END '
    
    # # Here, the script continues, and var is 3.
    Is this clearer now? Any advice now?

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Affected shell variable in awk

    Hello
    You can also do it like say, block the last statement END of your procedure AWK is add the following line:
    Code:
    END (
    ....
    ....
    ....
    printf ( "% s", var);
    )
    And in your script you call your awk
    Code:
    VAR = `awk-f script.awk ....`
    echo "VAR = $ VAR"
    This will help for sure!

  6. #6
    Join Date
    Oct 2009
    Posts
    67

    Re: Affected shell variable in awk

    Hi
    Thank you for your solution. I confess that I use the same exit (which limits the return has a numeric value less than 256), but I was looking if there was another way of doing things. I do not think this is possible, by the fact that the awk process can access the memory space of the script. Thanks again.

Similar Threads

  1. Change shell variable on Linux mint
    By Athreya in forum Operating Systems
    Replies: 3
    Last Post: 11-01-2011, 07:55 PM
  2. System affected with TR/BHO.qtk
    By Jecco in forum Networking & Security
    Replies: 3
    Last Post: 20-10-2009, 11:47 PM
  3. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  4. Replies: 3
    Last Post: 25-04-2009, 11:34 AM
  5. How to add a variable to a shell command in a vbscript
    By Dee in forum Windows Server Help
    Replies: 4
    Last Post: 28-04-2008, 09:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,256,904.65016 seconds with 17 queries