Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 带变量和特殊字符的sed或awk_Linux_Bash_Shell_Awk_Sed - Fatal编程技术网

Linux 带变量和特殊字符的sed或awk

Linux 带变量和特殊字符的sed或awk,linux,bash,shell,awk,sed,Linux,Bash,Shell,Awk,Sed,我想使用awk或sed替换文件中的一行: 我的文件内容是: server.modules += ( "mod_redirect" ) $SERVER["socket"] == ":8080" { $HTTP["host"] =~ "(.*)" { url.redirect = ( "^/(.*)" => "https://someurl.com/unauthorised" ) } } 我想更改包含url.redirect的行 新行在变量中,包含一些特殊字

我想使用awk或sed替换文件中的一行:

我的文件内容是:

server.modules += ( "mod_redirect" )

$SERVER["socket"] == ":8080" {
    $HTTP["host"] =~ "(.*)" {
        url.redirect = ( "^/(.*)" => "https://someurl.com/unauthorised" )
    }
}
我想更改包含
url.redirect的行
新行在变量中,包含一些特殊字符,如下所示
url.redirect=(“^/(.*)”=>”http://newurl.com/newpath“”

因此,我使用了以下sed命令:

sed "/url\.redirect =/s/.*/$newline/" 10-redirect.conf
但是我得到了与
换行符
变量中的特殊字符相关的错误

换行符是我的shell函数的一个参数,因此我不能更改它并在其中添加一些跳过字符


如何在
sed
awk
中使用具有特殊字符的变量?

您在
sed
中遇到错误,因为它使用的是正则表达式,您的替换字符串可能包含元字符,如
&
/
(分隔符)等

由于非正则表达式方法,此
awk
使用起来更安全:

newline='url.redirect = ( "^/(.*)" => "https://example.com/authorised" )'

awk -i inplace -v line="$newline" 'index($0, "url.redirect =") {
   sub(/[^[:blank:]].*/, "")
   $0 = $0 line
} 1' file

请注意,使用
ENVIRON
将允许使用所有shell特殊字符 awk:


你能试试下面的吗。这应该在新值前面放置与以前相同的空格

newline='url.redirect = ( "^/(.*)" => "https://example.com/authorised" )'
awk -v line="$newline" '
/url.redirect =/{
  match($0,/^ +/)
  print substr($0,RSTART,RLENGTH) line
  next
}
1
'  Input_file

使用
gnused
c
命令(用提供的字符串替换匹配的行)。如果字符串开头有空格,请加前缀
\
以保留空格

sed '/url\.redirect =/c\'"$newline"
但是,
c
命令仍然允许转义序列,例如:

$ s='   rat\tdog\nwolf'
$ seq 3 | sed '2c\'"$s"
1
   rat  dog
wolf
3

使用
r
命令,按字面意思和稳健方式添加内容

echo "$newline" | sed -e '/url\.redirect =/ {r /dev/stdin' -e 'd}'
下面是
r
命令的作用

$ s='   rat\tdog\nwolf'
$ echo "$s" | sed -e '2 {r /dev/stdin' -e 'd}' <(seq 3)
1
   rat\tdog\nwolf
3
$s='rat\tdog\nwolf'

$echo“$s”| sed-e'2{r/dev/stdin'-e'd}替换
$newline
中的换行符,以用于
\\n
。然后运行
sed
。或者最好使用一个临时文件,并使用
r
d
sed命令。尝试
sed'/url\.redirect=/c'$newline”
如果失败,尝试
echo“$newline”| sed-e'/url\.redirect=/{r/dev/stdin'-e'd}“
@KamilCuk在
$newline
中没有
\n
,请将
$newline
中的特殊字符替换为您理解的字符。例如,将
newline
变量中的换行符替换为两个字符:斜杠\和字符
n
@Sundeep。第一个解决方案是好的。但它删除了行开头的所有空格。您的解决方案很好。我只是在文件的末尾加了一张账单。如何删除它?我在缩进开始时有
“\t”
,现在删除了它。请再试一次。谢谢。你能补充更多的答案吗。在fcat中,我想直接替换文件,如
sed-I
。如何使用awk?只需在awk之后添加
-i in place
即可将更改保存到行中另一个变体是使用Bashs-here字符串
sed-e'/url\.redirect/r/dev/stdin'-e'/d'
echo "$newline" | sed -e '/url\.redirect =/ {r /dev/stdin' -e 'd}'
$ s='   rat\tdog\nwolf'
$ echo "$s" | sed -e '2 {r /dev/stdin' -e 'd}' <(seq 3)
1
   rat\tdog\nwolf
3