开发者

String comparison in awk

开发者 https://www.devze.com 2023-03-08 08:34 出处:网络
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:

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

精彩评论

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