如何使用AWK方法在BASH脚本上实现Startswith函数
我正在尝试使用AWK在BASH脚本中实现Startswith函数,但这是行不通的。
https://www.gnu.ornu.or.or.or.or.or.or.orgg/software/gawk/手动/html_node/string-functions.html
#! /bin/bash
starts_with()
{
local string=$1
local substring=$2
local index=$(printf "%s" "$string" | awk 'BEGIN { print index("$string", "$substring") }')
if [[ "$index" == "1" ]]
then
echo "true" : $string : $substring
else
echo "false" : $string : $substring
fi
}
starts_with key1="value" key1=
starts_with key2="value" wrong_key=
这是输出:
false : key1=value : key1=
false : key2=value : wrong_key=
但是尴尬页面说:
index(in, find)
Search the string in for the first occurrence of the string find, and return the position in characters where that occurrence begins in the string in. Consider the following example:
$ awk 'BEGIN { print index("peanut", "an") }'
-| 3
If find is not found, index() returns zero.
With BWK awk and gawk, it is a fatal error to use a regexp constant for find. Other implementations allow it, simply treating the regexp constant as an expression meaning ‘$0 ~ /regexp/’. (d.c.)
上面的starts_with_with
函数中有什么问题以及如何修复?
I'm trying to use awk to implement the startsWith function in bash script, but it is not work.
https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html
#! /bin/bash
starts_with()
{
local string=$1
local substring=$2
local index=$(printf "%s" "$string" | awk 'BEGIN { print index("$string", "$substring") }')
if [[ "$index" == "1" ]]
then
echo "true" : $string : $substring
else
echo "false" : $string : $substring
fi
}
starts_with key1="value" key1=
starts_with key2="value" wrong_key=
Here is the output:
false : key1=value : key1=
false : key2=value : wrong_key=
But the awk page says:
index(in, find)
Search the string in for the first occurrence of the string find, and return the position in characters where that occurrence begins in the string in. Consider the following example:
$ awk 'BEGIN { print index("peanut", "an") }'
-| 3
If find is not found, index() returns zero.
With BWK awk and gawk, it is a fatal error to use a regexp constant for find. Other implementations allow it, simply treating the regexp constant as an expression meaning ‘$0 ~ /regexp/’. (d.c.)
What's wrong in the above starts_with
function and how to fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您可以直接在bash中进行
awk
使用awk
毫无意义吗?There's no point in using
awk
when you can do it directly in bash?您无法在gnu
awk
命令中使用这样的shell变量,请考虑简单的示例输出
观察出现文字
$ string
出现,请使用-V
,如果您想要将浮动在壳中浮动的变量浮动到gnuawk
命令例如输出
(在GNU AWK 5.0.1中测试)
You can not use your shell variables like this in GNU
AWK
command consider simple exampleoutput
Observe that literal
$string
appeared, use-v
if you want to ram variable floating in your shell into GNUAWK
command for exampleoutput
(tested in GNU Awk 5.0.1)
或导出它,如果您不介意额外的键入:
如果您不想导出,这也有效,我想
我不知道Linux的剪贴板机制和其他人如何工作,但是如果您恰好使用
macOS
,以及您想带入的外壳变量并不过于复杂,这是另一种方法:如果您喜欢
getline
是单个语句,那么也许or export it if you don't mind the extra typing:
If u don't wanna export, this works too i suppose
I don't know how the clipboard mechanism for Linux and others work, but if you happen to be on
macOS
, and the shell variable you wanna bring in isn't overly complex, here's another method :And if you like your
getline
s to be single statement, then perhaps