Linux 基于conf文件替换文件的值
我有一个conf文件,其格式为variable=“value”,其中的值可能也有特殊字符。一个例子是: 第#D行=“(L#'id'='log”)和L#'id'为空” 我有另一个文件F,它应该替换基于这个conf文件的值。例如,如果F中有行 打印“$LINE\u D” 它应该被替换为 打印“(L#'id'=='log')和L#'id'为空” 如何在shell脚本中创建一个程序,它接受conf和F并生成F中的值Linux 基于conf文件替换文件的值,linux,perl,shell,unix,configuration,Linux,Perl,Shell,Unix,Configuration,我有一个conf文件,其格式为variable=“value”,其中的值可能也有特殊字符。一个例子是: 第#D行=“(L#'id'='log”)和L#'id'为空” 我有另一个文件F,它应该替换基于这个conf文件的值。例如,如果F中有行 打印“$LINE\u D” 它应该被替换为 打印“(L#'id'=='log')和L#'id'为空” 如何在shell脚本中创建一个程序,它接受conf和F并生成F中的值 谢谢您对所需内容的定义留下了很多空白,因此您可能需要调整此脚本。它是最初设计用于处理ma
谢谢您对所需内容的定义留下了很多空白,因此您可能需要调整此脚本。它是最初设计用于处理makefile的更复杂脚本的精简版本。这意味着可能有一些材料可以从这里移除而不会造成麻烦,尽管我已经摆脱了大部分无关的处理
#!usr/bin/env perl
#
# Note: this script can take input from stdin or from one or more files.
# For example, either of the following will work:
# cat config file | setmacro
# setmacro file
use strict;
use warnings;
use Getopt::Std;
# Usage:
# -b -- omit blank lines
# -c -- omit comments
# -d -- debug mode (verbose)
# -e -- omit the environment
my %opt;
my %MACROS;
my $input_line;
die "Usage: $0 [-bcde] [file ...]" unless getopts('bcde', \%opt);
# Copy environment into hash for MAKE macros
%MACROS = %ENV unless $opt{e};
my $rx_macro = qr/\${?([A-Za-z]\w*)}?/; # Matches $PQR} but ideally shouldn't
# For each line in each file specified on the command line (or stdin by default)
while ($input_line = <>)
{
chomp $input_line;
do_line($input_line);
}
# Expand macros in given value
sub macro_expand
{
my($value) = @_;
print "-->> macro_expand: $value\n" if $opt{d};
while ($value =~ $rx_macro)
{
print "Found macro = $1\n" if $opt{d};
my($env) = $MACROS{$1};
$env = "" unless defined $env;
$value = $` . $env . $';
}
print "<<-- macro_expand: $value\n" if $opt{d};
return($value);
}
# routine to recognize macros
sub do_line
{
my($line) = @_;
if ($line =~ /^\s*$/o)
{
# Blank line
print "$line\n" unless $opt{b};
}
elsif ($line =~ /^\s*#/o)
{
# Comment line
print "$line\n" unless $opt{c};
}
elsif ($line =~ /^\s*([A-Za-z]\w*)\s*=\s*(.*)\s*$/o)
{
# Macro definition
print "Macro: $line\n" if $opt{d};
my $lhs = $1;
my $rhs = $2;
$rhs = $1 if $rhs =~ m/^"(.*)"$/;
$MACROS{$lhs} = ${rhs};
print "##M: $lhs = <<$MACROS{$lhs}>>\n" if $opt{d};
}
else
{
print "Expand: $line\n" if $opt{d};
$line = macro_expand($line);
print "$line\n";
}
}
和另一个文件,F
,包含:
LINE_D="(L#'id' == 'log') AND L#'id' IS NULL"
PRINT '$LINE_D'
PRINT '${LINE_D}'
perl setmacro.pl cfg F
的输出为:
PRINT '(L#'id' == 'log') AND L#'id' IS NULL'
PRINT '(L#'id' == 'log') AND L#'id' IS NULL'
这与所需的输出匹配,但给了我带有多个单引号的heebie jeebies。然而,客户总是对的
(我想我已经去掉了剩余的Perl 4-ISM;基本脚本仍然有一些剩余部分,还有一些关于Perl 5.001如何以不同方式处理事情的注释。它确实使用了$`
和$'
,这通常不是一个好主意。但是它可以工作,因此修复它是读者的一项练习。regex变量现在不是一个好主意必要的;它也在识别make
宏符号-$(宏)
以及${macro}
)的时候