I am trying to perform this regex with perl

info=$(echo $info | perl -p -e 's/(?:\d[\s-.]*){12,19}/**********/g')

Trying to match any digits range with 12 to 19 regardless of whitespaces,-, or . characters

When I perform this on $info, and let's say info looks like this

<ns4:SpecialRequest>** LoyalMember #1234123412341234 **</ns4:SpecialRequest>

For some odd reason, it sets SpecialRequest into a string with all of the names of the files on my current directory. Like an ls command. Let's say my ls outputs

awscli-bundle.zip        script.sh        item.csv        misc.gz

Then the resulting info turns into

<ns4:SpecialRequest>* awscli-bundle.zip item.csv LoyalMember misc.gz script.sh #********** **</ns4:SpecialRequest>

Even if I dont use wildcard to replace, but using letter x, somehow the * (wildcard with space within the xml value or in the replacment) causes the file names in directory to pop into the replace string...

info=$(echo $info | perl -p -e 's/(?:\d[\s-.]*){12,19}/xxxxxxxxxx/g')

<ns4:SpecialRequest>* awscli-bundle.zip item.csv LoyalMember misc.gz script.sh #xxxxxxxxxx **</ns4:SpecialRequest>

Added more code

info=$(echo $info | perl -p -e 's/(?:\d[-\s.,]*){12,16}/xxxxxxxxxxxxxx/g')
#replace thru xml
xmlstarlet ed --inplace -u "(//*[local-name()='SpecialRequest'])[$i]" -v "${info}" ${s3file}
  • 7
    This isn't about perl, it's about what you do with the $inf variable in the shell. – glenn jackman Jun 17 at 17:54
  • 3
    See Security implications of forgetting to quote a variable in bash/POSIX shells and all the other Q&A referenced in there. You probably did a echo $inf instead of printf '%s\n' "$inf" to check the contents of the variable. – Stéphane Chazelas Jun 17 at 18:20
  • As Glenn said, this is about how you use your inf variable later in your script. However, I would want to see more of the document structure as I think your issue could be better solved with tools made for parsing XML documents. – Kusalananda Jun 17 at 18:34
  • 1
    In the code that you added, $info is used unquoted with echo. In the code that you show at the top of the question, $info is used quoted with echo. I find this difference confusing and it means that we really can't trust that we are seeing the code that you are actually running. Please take a while and fix your text so that it is correct and consistent. – Kusalananda Jun 17 at 18:51
  • 1
    If you want to do this in perl, you should probably use one of the perl XML parsing modules, like XML::Parser or XML::TokeParser. For simple things, you can get away with a shell script that runs sed or perl or xmlstarlet or whatever to extract/manipulate data but it won't be fast (especially if you need to do all that multiple times in a loop), and you'll be continually confusing yourself with shell quoting issues. For more complex tasks, write the entire thing in perl, or python, or whatever. – cas Jun 18 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.