如何使用AWK方法在BASH脚本上实现Startswith函数

发布于 2025-01-21 20:48:28 字数 1420 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总以为 2025-01-28 20:48:28

当您可以直接在bash中进行awk使用awk毫无意义吗?

#!/bin/bash
starts_with() {
    local string="$1"
    local substring="$2"

    if [[ $string == "$substring"* ]]
    then
        echo "true : $string : $substring"
    else
        echo "false : $string : $substring"
    fi
}

There's no point in using awk when you can do it directly in bash?

#!/bin/bash
starts_with() {
    local string="$1"
    local substring="$2"

    if [[ $string == "$substring"* ]]
    then
        echo "true : $string : $substring"
    else
        echo "false : $string : $substring"
    fi
}
痴情 2025-01-28 20:48:28
awk 'BEGIN { print index("$string", "$substring") }'

您无法在gnu awk命令中使用这样的shell变量,请考虑简单的示例

string="HELLOWORLD" && awk 'BEGIN{print "$string"}' emptyfile.txt

输出

$string

观察出现文字$ string出现,请使用-V,如果您想要将浮动在壳中浮动的变量浮动到gnu awk命令例如

string="HELLOWORLD" && awk -v s=$string 'BEGIN{print s}' emptyfile.txt

输出

HELLOWORLD

(在GNU AWK 5.0.1中测试)

awk 'BEGIN { print index("$string", "$substring") }'

You can not use your shell variables like this in GNU AWK command consider simple example

string="HELLOWORLD" && awk 'BEGIN{print "$string"}' emptyfile.txt

output

$string

Observe that literal $string appeared, use -v if you want to ram variable floating in your shell into GNU AWK command for example

string="HELLOWORLD" && awk -v s=$string 'BEGIN{print s}' emptyfile.txt

output

HELLOWORLD

(tested in GNU Awk 5.0.1)

永言不败 2025-01-28 20:48:28

或导出它,如果您不介意额外的键入:

export string="HELLOWORLD" && mawk '

BEGIN { 
       print ENVIRON["string"]
}'

HELLOWORLD

如果您不想导出,这也有效,我想

% string2="HELLOWORLD" mawk '

  BEGIN{ 

    print ENVIRON["string2"] 

  }' | gtee >( gpaste - \
    \
     | gsed -zE 's/^/ orig from pipe :: /' >&2;) | gcat -n       
           
     1  HELLOWORLD
 orig from pipe :: HELLOWORLD 

我不知道Linux的剪贴板机制和其他人如何工作,但是如果您恰好使用macOS,以及您想带入的外壳变量并不过于复杂,这是另一种方法:

pbcopy <<<"${TERM_SESSION_ID}" ;

mawk 'BEGIN { 
              (__=" pbpaste ") | getline _;
         close(__) 

print "pasting via macos clipboard ::", (_) }' | lgp3

pasting via macos clipboard :: BEC24EEE-B6B3-4862-A286-1953FB6438A7

如果您喜欢getline是单个语句,那么也许

 mawk ' BEGIN { 
 
   printf(" pasting via macos clipboard :: %*s",
            close((__="pbpaste")|getline _)<"",_) 
 
 }' | lgp3 

 pasting via macos clipboard :: BEC24EEE-B6B3-4862-A286-1953FB6438A7

or export it if you don't mind the extra typing:

export string="HELLOWORLD" && mawk '

BEGIN { 
       print ENVIRON["string"]
}'

HELLOWORLD

If u don't wanna export, this works too i suppose

% string2="HELLOWORLD" mawk '

  BEGIN{ 

    print ENVIRON["string2"] 

  }' | gtee >( gpaste - \
    \
     | gsed -zE 's/^/ orig from pipe :: /' >&2;) | gcat -n       
           
     1  HELLOWORLD
 orig from pipe :: HELLOWORLD 

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 :

pbcopy <<<"${TERM_SESSION_ID}" ;

mawk 'BEGIN { 
              (__=" pbpaste ") | getline _;
         close(__) 

print "pasting via macos clipboard ::", (_) }' | lgp3

pasting via macos clipboard :: BEC24EEE-B6B3-4862-A286-1953FB6438A7

And if you like your getlines to be single statement, then perhaps

 mawk ' BEGIN { 
 
   printf(" pasting via macos clipboard :: %*s",
            close((__="pbpaste")|getline _)<"",_) 
 
 }' | lgp3 

 pasting via macos clipboard :: BEC24EEE-B6B3-4862-A286-1953FB6438A7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文