How to change the format of substitution variables in a template

I need to iterate over files in a directory and perform the following replacement.

Before:

Hello $ {USER_NAME}, you live in $ {HOME_ADDRESS}. Now it's $ {TIME}

After:

Hello $ {username}, you live in $ {HomeAddress}. Now it's $ {time}

The number of different tokens that appear in $ {} is large, so it is impossible to execute it:

find . -name '*' -exec sed -i 's/${USER_NAME}/${userName}/g' {} \;
find . -name '*' -exec sed -i 's/${TIME}/${time}/g' {} \;

and etc.

I hope you can do this replacement with one command, which looks something like this:

find . -name '*' -exec sed 's/XXX/YYY/g' {} \;

But I can not understand what to replace XXX and YYY. Is it possible to do this in one team?

Cheers, Donal

+3
source share
3 answers

A flag -ifor sed will edit the file in place. For XXX and YYY you should use something like:

sed -i 's/USER_NAME/userName/g'

etc.

. , "USER_NAME" "userName" . Perl script:

sub convert {
    my $r = lc $_[0];
    $r =~ s/_(.)/\U$1\E/g;
    return $r;
}
while (<>) {
    s/\${([A-Z_]+)}/\${@{[convert $1]}}/g;
    print;
}

:

perl -i convert.pl inputfile.txt

:

$ cat inputfile.txt
Hello ${USER_NAME}, you live at ${HOME_ADDRESS}. It is now ${TIME}
$ perl -i convert.pl inputfile.txt
$ cat inputfile.txt
Hello ${userName}, you live at ${homeAddress}. It is now ${time}
+2

:

sed -i '/^Hello/ { s/\$\{USER_NAME\}/\$\{userName\}/g 
                   s/\$\{HOME_ADDRESS\}/\$\{homeAddress\}/g 
                   s/\$\{TIME\}/\$\{time\}/g
                  }'

/^Hello/ , ( , ), .


script, HERE ...

+4

. , :

sed '/^Hello/ { s/\$\{USER_NAME\}/\$\{userName\}/g' <filename> \
   | sed 's/\$\{HOME_ADDRESS\}/\$\{homeAddress\}/g' \
   | sed 's/\$\{TIME\}/\$\{time\}/g'

( , stdout, - ( , , e, ). , sed,

!! | sed 'yet-enother-regexp'

? VI-? ?:)

0

Source: https://habr.com/ru/post/1698771/


All Articles