string awk 中的字符串比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6139063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String comparison in awk
提问by Dagang
I need to compare two strings in alphabetic order, not only equality test. I want to know is there way to do string comparison in awk?
我需要按字母顺序比较两个字符串,而不仅仅是相等性测试。我想知道有没有办法在 awk 中进行字符串比较?
回答by paxdiablo
Sure it can:
当然可以:
pax$ echo 'hello
goodbye' | gawk '{if (pax> printf 'aaa\naab\naac\naad\n' | gawk '{if ( < "aac"){print}}'
aaa
aab
== "hello") {print "HELLO"}}'
HELLO
You can also do inequality(ordered) testing as well:
您还可以进行不等式(有序)测试:
echo aaa bbb | awk '{ print ( >= ) ? "true" : "false" }'
回答by Ilya Matveychikov
You can do string comparison in awk using standard boolean operators, unlike in C where you would have to use strcmp().
您可以使用标准布尔运算符在 awk 中进行字符串比较,这与在 C 中必须使用 strcmp() 不同。
echo "xxx yyy" > test.txt
echo "xxx yyy" > test.txt
cat test.txt | awk '$1!=$2 { print($1 $2); }'
cat test.txt | awk '$1!=$2 { print($1 $2); }'
回答by Ian Chang
You can check the answer in the nawk manual
您可以在nawk 手册中查看答案
##代码##