bash awk - 未终止的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7584371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:53:13  来源:igfitidea点击:

Awk - unterminated regex

bashawksh

提问by BandyOrc

I am writing a shell script which needs to pull values out of a text file which looks like this:

我正在编写一个 shell 脚本,它需要从如下所示的文本文件中提取值:

app.full.name /warfilelocation/ warfilename

My shell script will be iterating over a list of application names and pulling out either the location or name using AWK. I have tested doing this on the command line using the following: awk "\$1 ~/app.full.name/ { print $2 }" applications.txt

我的 shell 脚本将遍历应用程序名称列表并使用 AWK 提取位置或名称。我已经使用以下命令在命令行上进行了测试: awk "\$1 ~/app.full.name/ { print $2 }" applications.txt

which returns what I would expect however when i put this in a shell script I start having issues.

这会返回我期望的结果,但是当我将其放入 shell 脚本时,我开始遇到问题。

I have a function that looks like this:

我有一个看起来像这样的函数:

function get_location() {
        local application=
        awk "$1 ~/^$application/ { print $2 }"  applications.txt 
}

But when i call this function i get the following error:

但是当我调用这个函数时,我收到以下错误:

awk:  ~/^app.full.name
awk:      ^ unterminated regexp
awk: cmd. line:1: app.full.name
awk: cmd. line:1:         ^ syntax error
awk: cmd. line:2: app.full.name/ { print  }
awk: cmd. line:2:    ^ syntax error

Does anyone have any ideas what I am doing wrong here. I presume I am not escaping the variable correct but no matter what i try it doesnt seem to work.

有没有人知道我在这里做错了什么。我想我没有正确地转义变量,但无论我尝试什么,它似乎都不起作用。

Thanks in advance

提前致谢

回答by Fredrik Pihl

Use this approach to make awk recognize shell variables:

使用这种方法让 awk 识别 shell 变量:

awk -v "v1=$VAR1" -v "v2=$VAR2" '{print v1, v2}' input_file

Update

更新

$ cat input
tinky-winky
dipsy
laa-laa
noo-noo
po

$ teletubby='po'

$ awk -v "regexp=$teletubby" '
kent$  app=app.ful
kent$  echo "app.full.name /warfilelocation/ warfilename"|awk -v a=$app '{if(match(,a))print }' 
/warfilelocation/
~ regexp' input po

Note that anythingcould go into the shell-variable, even a full-blown regexp, e.g ^d.*y. Just make sure to use single-quotes to prevent the shell from doing any expansion.

请注意,任何东西都可以进入 shell 变量,即使是完整的正则表达式,例如^d.*y. 只要确保使用单引号来防止 shell 进行任何扩展。

回答by Kent

see this: using awk match() function

看到这个:使用 awk match() 函数

$ export application=foo/bar
$ awk "$1 ~/^$application/ { print $1 }"
gawk: cmd. line:1:  ~/^foo/bar/ { print  }
gawk: cmd. line:1:                ^ parse error

回答by Matthew Farwell

It's hard to tell without knowing exactly the value of $application, but it seems like you have a strange character in $application, such as a " or a / or something like that.

在不确切知道 $application 的值的情况下很难判断,但似乎 $application 中有一个奇怪的字符,例如 " 或 / 或类似的东西。

$ export application=`echo foo/bar | sed -e 's;/;\\/;g'`
$ awk "$1 ~/^$application/ { print $1 }"

I would look at the exact value that you have in $application, and if it contains a /, escape it.

我会查看您在 $application 中的确切值,如果它包含 /,则将其转义。

One way to do this would be to use:

一种方法是使用:

##代码##

回答by thiton

The error messages seem to indicate that there is a stray newline at the end of $application, which gives the "line 2" error messages.

错误消息似乎表明 $application 末尾有一个杂散的换行符,它给出了“第 2 行”错误消息。