Position of a string within a string using Linux shell script?

后端 未结 4 1117
逝去的感伤
逝去的感伤 2020-12-05 06:35

If I have the text in a shell variable, say $a:

a=\"The cat sat on the mat\"

How can I search for \"cat\" and return 4 using a

4条回答
  •  情深已故
    2020-12-05 07:00

    I used awk for this

    a="The cat sat on the mat"
    test="cat"
    awk -v a="$a" -v b="$test" 'BEGIN{print index(a,b)}'
    

提交回复
热议问题