2

我想grep在 Linux bash 脚本的命令中传递一个变量。变量是来自 Internet 的文本文件,我想在其中找到一些单词。

我在 bash 中尝试了以下命令:

  1. cat "$var" | grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)'
  2. echo "$var" | grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)'
  3. grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' <<< "$var"

但我没有得到正确的结果。我该怎么做?

这是我的狂欢:

#!/bin/sh    
urltext=$(curl -s https://example.com)
string=$(grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' "$urltext" | tr '.' '\n' )
cat $string
4

1 回答 1

0

What's supposed to be contained in the variable ?

It's a file ?

grep <options> <expression> "$var"

It's a string ?

echo "$var"|grep <options> <expression>
grep <options> <expression> <(echo "$var")

NB : try -e option if there are several lines in $var

于 2013-10-29T22:06:31.083 回答