mistake of using awk

This is a discussion on mistake of using awk within the Linux Programming forums, part of the Platform Specific Boards category; Hi, Here is my code, Code: ids=(1 5 10 15 20 25 30 35 40 45 50 55 60 65 ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    mistake of using awk

    Hi,
    Here is my code,
    Code:
    ids=(1 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 90 100 110 120)
    for ((i=0;i<${#ids};i++)); do
       awk '{ print ${ids[${i}]}" "$1; }' > ${TMP} < ./results/$1${ids[${i}]}/test_error.dat
    done
    I got this error
    awk: { print ${ids[${i}]}" "$1; }
    awk: ^ syntax error
    awk: { print ${ids[${i}]}" "$1; }
    awk: ^ syntax error
    awk: fatal: invalid subscript expression
    Could you please explain what mistake I made? Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Shell variables aren't available within awk. What shell are you using?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by lehe View Post
    Code:
       awk '{ print ${ids[${i}]}" "$1; }'
    Those single quotes are preventing shell variable expansion. Try using double quotes instead, and escape the inner double quotes and awk variable $:

    Code:
    awk "{ print ${ids[${i}]}\" \"\$1; }"
    EDIT: Note that your use of $1 in the results redirect won't work, because $1 is an awk variable which can't be accessed from the shell level.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    I am using bash.
    The $1 in
    Code:
    '{ print ${ids[${i}]}" "$1; }'
    was intended to be read by awk as the first field in the file
    Code:
    "./results/$1${ids[${i}]}/test_error.dat"
    , where the $1was intended to be the 1st argument from command line calling this bash script.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by lehe View Post
    I am using bash.
    The $1 in
    Code:
    '{ print ${ids[${i}]}" "$1; }'
    was intended to be read by awk as the first field in the file
    Code:
    "./results/$1${ids[${i}]}/test_error.dat"
    , where the $1was intended to be the 1st argument from command line calling this bash script.
    Ah. In that case, I think just changing the quoting should work.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks!
    What I actually want to do is to read from a bunch of files test_error.dat each of which cantains single number, and write them into a single file "${TMP}" as
    1 num1
    5 num5
    10 num10
    15 num15
    ...
    Each row has a space between the two numbers. That's why I put a double-quoted space in print before $1.

    If I change single quote to double quote as
    Code:
    ids=(1 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 90 100 110 120)
    for ((i=0;i<${#ids};i++)); do
       awk "{ print ${ids[${i}]}" "$1; }" > ${TMP} < ./results/$1${ids[${i}]}/test_error.dat
    done
    I will get
    awk: cmd. line:1: { print 1
    awk: cmd. line:1: ^ unexpected newline or end of string

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by lehe View Post
    Thanks!
    What I actually want to do is to read from a bunch of files test_error.dat each of which cantains single number, and write them into a single file "${TMP}" as
    1 num1
    5 num5
    10 num10
    15 num15
    ...
    If that's the case try this script:
    Code:
    awk '{print $1, "num"$1}' test_error.dat > outputfile

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help! - Who find mistake in the program??
    By nivek in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 12:28 AM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Point Out Mistake!!!
    By RahulDhanpat in forum C Programming
    Replies: 14
    Last Post: 04-22-2006, 08:14 PM
  4. Please assistance, general protection mistake
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 10:45 PM
  5. Whom do you blame for the mistake in Pres. Bush's State of the Union speech?
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-15-2003, 07:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21