0

i have a problem with this code.. i can't figure out what i have to write as condition to cut my file with awk.

i=0    
while [ $i -lt 10 ]; #da 1 a 9, Ap1..Ap9
    do

      case $i in
        1) RX="54:75:D0:3F:1E:F0";;
        2) RX="54:75:D0:3F:4D:00";;
        3) RX="54:75:D0:3F:51:50";;
        4) RX="54:75:D0:3F:53:60";;
        5) RX="54:75:D0:3F:56:10";;
        6) RX="54:75:D0:3F:56:E0";;
        7) RX="54:75:D0:3F:5A:B0";;
        8) RX="54:75:D0:3F:5F:90";;
        9) RX="D0:D0:FD:68:BC:70";;
        *) echo "Numero invalido!";;
      esac
      echo "RX = $RX" #check 
      awk -F, '$2 =="$RX" { print $0 }' File1 > File2[$i] #this is the line!
  i=$(( $i + 1 ))
  done

the command echo prints correctly but when i use the same "$RX" as condition in AWK it doesn't work (it prints a blank page). my File1 :

1417164082794,54:75:D0:3F:53:60,54:75:D0:3F:1E:F0,-75,2400,6 1417164082794,54:75:D0:3F:56:10,54:75:D0:3F:1E:F0,-93,2400,4 1417164082794,54:75:D0:3F:56:E0,54:75:D0:3F:1E:F0,-89,2400,4 1417164082794,54:75:D0:3F:5A:B0,54:75:D0:3F:1E:F0,-80,2400,4 1417164082794,54:75:D0:3F:53:60,54:75:D0:3F:1E:F0,-89,5000,2

could you tell me the right expression "awk -F ..."

thank you very much!

4
  • 1
    Variables aren't expanded within single quotes. Commented Apr 1, 2015 at 10:40
  • variables in awk are different from variables in bash Commented Apr 1, 2015 at 10:41
  • '$2 =="'"$RX"'" { print $0 }' Commented Apr 1, 2015 at 10:46
  • @anishane no, never do that. See cfajohnson.com/shell/cus-faq-2.html#Q24 for the right ways to do it. Commented Apr 1, 2015 at 15:24

2 Answers 2

3

To pass variables from shell to awk use -v:

awk -F, -v R="$RX" '$2 ==R { print $0 }' File1 > File2[$i]
Sign up to request clarification or add additional context in comments.

1 Comment

and get rid of the { print $0 } as that's the default behavior so it's useless to specify it explicitly.
0

@Ricky - any time you write a loop in shell just to manipulate text you have the wrong approach. It's just not what the shell was created to do - it's what awk was created to do and the shell was created to invoke commands like awk.

Just use a single awk command and instead of reading File 10 times and switching on variables for every line of the file, just do it all once, something like this:

BEGIN {
    split(file2s,f2s)
    split("54:75:D0:3F:1E:F0\
           54:75:D0:3F:4D:00\
           54:75:D0:3F:51:50\
           54:75:D0:3F:53:60\
           54:75:D0:3F:56:10\
           54:75:D0:3F:56:E0\
           54:75:D0:3F:5A:B0\
           54:75:D0:3F:5F:90\
           D0:D0:FD:68:BC:70", rxs)
    for (i in rxs) {
        rx2file2s[rxs[i]] = f2s[i]
    }
}
{
    if ($2 in rx2file2s) {
        print > rx2file2s[$2]
    }
    else {
        print NR, $2, "Numero invalido!" | "cat>&2"
    }
}

which you'd then invoke as awk -v file2s="${File2[@]}" -f script.awk File1

I say "something like" because you didn't provide any sample input (File1 contents) or expected output (File2* values and contents) so I couldn't test it but it will be very close to what you need if not exactly right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.