-1

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?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
stackbiz
  • 1,136
  • 1
  • 5
  • 22
  • See [how-do-i-use-shell-variables-in-an-awk-script](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) – Ed Morton Apr 17 '22 at 13:28

3 Answers3

1

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
}
Fravadona
  • 13,917
  • 1
  • 23
  • 35
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)

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

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
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11