简体   繁体   English

Bash脚本awk条件

[英]Bash Script Awk Condition

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. 我对此代码有问题..我不知道我要写什么作为用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). 命令回显可以正确打印,但是当我在AWK中使用相同的“ $ RX”作为条件时,它将不起作用(它将打印空白页)。 my File1 : 我的文件1:

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 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 ..." 你能告诉我正确的表达方式“ awk -F ...”

thank you very much! 非常感谢你!

要将变量从shell传递到awk,请使用-v:

awk -F, -v R="$RX" '$2 ==R { print $0 }' File1 > File2[$i]

@Ricky - any time you write a loop in shell just to manipulate text you have the wrong approach. @Ricky-每当您在shell中编写一个循环来仅仅操纵文本时,您都会使用错误的方法。 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. 这不是创建shell要做的,而是创建awk要做的,创建shell来调用诸如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: 只需使用单个awk命令,而不是读取File 10次​​并为文件的每一行打开变量,只需全部执行一次,就像这样:

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 然后将其作为awk -v file2s="${File2[@]}" -f script.awk File1调用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. 我之所以说“类似”,是因为您没有提供任何示例输入(File1内容)或预期输出(File2 *值和内容),所以我无法对其进行测试,但如果不完全正确,它将非常接近您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM