如何在ksh中的变量上进行精确的单词匹配? [英] How do I do an exact word match on a variable in ksh?

查看:92
本文介绍了如何在ksh中的变量上进行精确的单词匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下语法用作ksh脚本的一部分,以验证LINE_FROM_FILE中是否存在单词Validation.

I used the following syntax as part of a ksh script to verify if the word Validation exists in LINE_FROM_FILE.

[[ "${LINE_FROM_FILE##*Validation}" != "${LINE_FROM_FILE}" ]] && print "match Validation"

这种语法的问题在于它还与ValidValidationVALID等单词匹配,而我的目标是与变量$LINE_FROM_FILE中的单词Validation完全匹配.

The problem of this syntax is that it is also matching words like Valid or ValidationVALID etc. and my goal is to exactly match the word Validation in the variable $LINE_FROM_FILE.

我问是否还可以在脚本中使用Perl语法来完全匹配单词Validation,例如:

I ask if it is also possible to use Perl syntax in my script to exactly match the word Validation, for example:

[[ ` some perl command ` = Validation  ]] && print "match Validation"

推荐答案

测试行是否在单词分隔字符之间包含单词.一个有用的技巧是在字符串的开头和结尾添加一个以单词分隔的字符,以防单词位于字符串的开头或结尾.

Test whether the line contains the word between word-separating characters. A helpful trick is to add a word-separating character at the beginning and at the end of the string, in case the word is at the beginning or end of the string.

[[ " $LINE_FROM_FILE " == *[![:alnum:]]Validation[![:alnum:]]* ]]

这假定单词仅由字母和数字组成.如果定义不同,请调整图案.

This assumes that words consist of letters and digits only. Adjust the pattern if you have a different definition.

请注意,您编写的测试[[ "${LINE_FROM_FILE##*Validation}" != "${LINE_FROM_FILE}" ]]是编写[[ $LINE_FROM_FILE = *Validation* ]]的一种复杂方法(即,将Validation作为子字符串进行检查).

Note that the test you wrote, [[ "${LINE_FROM_FILE##*Validation}" != "${LINE_FROM_FILE}" ]], is a complicated way of writing [[ $LINE_FROM_FILE = *Validation* ]] (i.e., checking for Validation as a substring).

这篇关于如何在ksh中的变量上进行精确的单词匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆