我正在处理日志存档脚本。
我在一台机器上有三个目录
/opt/tibco/run/tibbpma/tibcohost/BPM_DEV_TIBBPMA_HOST_01/host/logs
/opt/tibco/run/tibbpmb/tibcohost/BPM_DEV_TIBBPMB_HOST_01/host/logs
/opt/tibco/run/tibbpmc/tibcohost/BPM_DEV_TIBBPMC_HOST_01/host/logs
脚本使用for循环进入每个目录并查找带有模式 .log。的所有日志,并将附加日期的文件移动到/opt/tibco/scripts/logs/archive
下的公共目录
3个/opt/tibco/run/*/*/*/logs
目录下的日志名称有时相同,并在移动时被相互覆盖。所以为了避免覆盖,我想在文件名中添加tibbpma_BPM_DEV_TIBBPMA_HOST_01_
以获得不同的文件名。
请帮助我们获取tibbpma_BPM_DEV_TIBBPMA_HOST_01_
或tibbpmb_BPM_DEV_TIBBPMB_HOST_01_
或tibbpmc_BPM_DEV_TIBBPMC_HOST_01_
答案 0 :(得分:0)
您可以使用此grep -o -P 'BPM[^/]+'
从这些路径中提取所需的部分,例如
$ echo /opt/tibco/run/tibbpma/tibcohost/BPM_DEV_TIBBPMA_HOST_01/host/logs | grep -o -P 'BPM[^/]+'
BPM_DEV_TIBBPMA_HOST_01
答案 1 :(得分:0)
您可以使用此正则表达式:
\/opt\/tibco\/run\/([^\/]*)\/([^\/]*)\/([^\/]*)\/host\/logs
对于/ opt / tibco / run / tibbpma / tibcohost / BPM_DEV_TIBBPMA_HOST_01 / host / logs
$1 would return tibbpma
$2 would return tibcohost
$3 would return BPM_DEV_TIBBPMA_HOST_01
然后,您可以使用$ 1,$ 2和$ 3
制作所需的字符串示例perl代码,用于提供您要查找的字符串
my $str = "/opt/tibco/run/tibbpma/tibcohost/BPM_DEV_TIBBPMA_HOST_01/host/logs";
if ($str =~ /\/opt\/tibco\/run\/([^\/]*)\/([^\/]*)\/([^\/]*)\/host\/logs/) {
print $1 . "_" . $2 . "_". $3;
}
将输出设为tibbpma_tibcohost_BPM_DEV_TIBBPMA_HOST_01 如果你想省略,tibcohost part ..要么从上面的正则表达式中省略$ 2
OR
使用RE / opt / tibco / run /([^ /] )/ [^ /] /([^ /] *)/ host / logs
my $str = "/opt/tibco/run/tibbpma/tibcohost/BPM_DEV_TIBBPMA_HOST_01/host/logs";
if ($str =~ /\/opt\/tibco\/run\/([^\/]*)\/[^\/]*\/([^\/]*)\/host\/logs/) {
print $1 . "_" . $2 ;
}
获取tibbpma_BPM_DEV_TIBBPMA_HOST_01