我不熟悉sed,需要在许多大型日志文件中更改数百个主机名
例如
URL:http://test-wls-1.compute-1234.cloud.internal .Response code: 503>
我需要将其更改为
URL:http[s]://hostname.compute-1234.cloud.internal: .Response code: 503>
我尝试使用sed regex
s'/http[s]\?:\/\/[^ ]./http[s]:\/\/hostname/'
但是当主机中的破折号被当作单词返回时
URL:http[s]://hostname-wls-1.compute-1234.cloud.internal .Response code: 503>
因此需要一点帮助来了解我要去哪里哪里
谢谢提前
您可以使用
sed 's~\(https\{0,1\}://\)[^.]\{1,\}~\1hostname~' # POSIX BRE
sed -E 's~(https?://)[^.]+~\1hostname~' # POSIX ERE
请参见an online demo:
s='URL:http://test-wls-1.compute-1234.cloud.internal .Response code: 503> '
sed 's~\(https\{0,1\}://\)[^.]\{1,\}~\1hostname~' <<< "$s"
# => URL:http://hostname.compute-1234.cloud.internal .Response code: 503>
详细信息
\(https\{0,1\}://\)
-组1(在替换部分中称为\1
):http
或https
,然后是://
字符串[^.]\{1,\}
-.
以外的1个或多个字符\1hostname
-(RHS):第1组值和hostname
子字符串。使用不同的定界符(不必为/
),这样就不必转义许多斜线。我将使用|
作为分隔符,此正则表达式将执行以下操作:
sed 's|http[s]\?://[^.]*|http[s]://hostname|'
http[s]\?://[^.]*
获取介于http[s]://
和下一个点字符(在您的情况下为http://test-wls-1
)之间的字符串,并将其转换为http[s]://hostname
,得到:
$ echo 'URL:http://test-wls-1.compute-1234.cloud.internal .Response code: 503>' |
sed 's|http[s]\?://[^.]*|http[s]://hostname|'
URL:http[s]://hostname.compute-1234.cloud.internal .Response code: 503>