小编Spa*_*196的帖子

sed给出的前面的正则表达式无效

我一直在处理一个sed脚本文件,当我运行它时,我遇到了"Invalid Preceding regular expression"错误.以下是整个文件.

我已经在这个网站和其他地方进行了很多搜索.这里提出的许多问题都导致需要扩展正则表达式,而不正确地转义.我已将此定义为扩展表达式,因为它是电子邮件替换所需的.

#!/bin/sed -rf
#/find_this thing/{
#s/ search_for_this/ replace_with_this/
#s/ search_for_this_other_thing/ replace_with_this_other_thing/
#}

#Search and replace #ServerAdmin (with preceding no space) email addresses using a regular expression that has the .com .net and so on domain endings as option so it will find root@localhost and replace it in line with admin's email address.

ServerAdmin/ { 
s/\b[A-Za-z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/
}
#Enable user's Public HTML directories
/UserDir/ {
s/disable$/enable/ 
s/^#User/User/
}
#Replace the only #ServerName (with preceding no space) followed space …
Run Code Online (Sandbox Code Playgroud)

regex bash command-line sed

13
推荐指数
1
解决办法
3万
查看次数

使用正则表达式对哈希进行线分割

我试图在ubuntu中创建一个基于文件/etc/mime.types的哈希,例如,它将白色空间上的每一行和"非单词"正则表达式搜索分开.以下是我的代码.但我不能让它正确输出零值的.他们把所有其他东西放在一起,但是如果你用pp打印它们,有些人会回复为零.有什么建议?

#!/usr/bin/ruby
mime = Hash.new()

File.open("/etc/mime.types", "r") do |file|
  file.each_line do |line|
    next if line[0] == ?#
    next if line == "\n"
    key, value = line.chomp.split(/\W+\s/)
    mime[key] = value
  end
end

if (mime.has_value?('nil') == true)
    mime.sort.each {|key,value| puts "#{key} has no extensions."}
elsif(mime.has_value?('nil') == false)
    mime.sort.each {|key, value| puts "#{key} has extensions #{value}"}
end
Run Code Online (Sandbox Code Playgroud)

这只是输出的尾端,因为它在执行时位于前面,所以最容易看到

我预计

video/quicktime has extensions qt mov
**video/vnd.fvt has no extensions** 
**video/vnd.motorola.video has no extensions 
video/vnd.motorola.videop has no extensions** 
video/vnd.mpegurl has extensions mxu
**video/vnd.mts has no …
Run Code Online (Sandbox Code Playgroud)

ruby hash split line

1
推荐指数
1
解决办法
311
查看次数

标签 统计

bash ×1

command-line ×1

hash ×1

line ×1

regex ×1

ruby ×1

sed ×1

split ×1