开发者

String comparison in awk

I need to compare two s开发者_如何学编程trings in alphabetic order, not only equality test. I want to know is there way to do string comparison in awk?


Sure it can:

pax$ echo 'hello
goodbye' | gawk '{if ($0 == "hello") {print "HELLO"}}'
HELLO

You can also do inequality (ordered) testing as well:

pax> printf 'aaa\naab\naac\naad\n' | gawk '{if ($1 < "aac"){print}}'
aaa
aab


You can do string comparison in awk using standard boolean operators, unlike in C where you would have to use strcmp().

echo "xxx yyy" > test.txt

cat test.txt | awk '$1!=$2 { print($1 $2); }'


You can check the answer in the nawk manual

echo aaa bbb | awk '{ print ($1 >= $2) ? "true" : "false" }'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜