简体   繁体   English

awk 中的字符串比较

[英]String comparison in awk

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 中进行字符串比较?

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().您可以使用标准 boolean 运算符在 awk 中进行字符串比较,这与在 C 中必须使用 strcmp() 不同。

echo "xxx yyy" > test.txt

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

You can check the answer in the nawk manual您可以在nawk 手册中查看答案

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

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

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