Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:08:13+00:00 2026-06-11T18:08:13+00:00

I am trying to substitute few special characters using sed/perl for which I am

  • 0

I am trying to substitute few special characters using sed/perl for which I am getting errors.

In below command i am trying to replace values for SEARCHFIELDAPP, OLD_STRING,NEW_STRING etc;

Server1> SERVER_COMMAND="perl -F'\|' -i -lape  'if (\$F[0] eq \"SEARCHFIELDAPP\" && \$F[1] eq \"SEARCHFIELDDB\" && \$F[6] eq \"SEARCHFIELDUNM\" )  { s/\|OLD_STRING\|/\|NEW_STRING\|/g; s/\|LAST_UPDATE_DT/\|CURR_UPDATE_DT/g }' " 

Above command is as shown below when printed on console.

Server1> echo $SERVER_COMMAND
perl -F'\|' -i -lape 'if ($F[0] eq "SEARCHFIELDAPP" && $F[1] eq "SEARCHFIELDDB" && $F[6] eq "SEARCHFIELDUNM" ) { s/\|OLD_STRING\|/\|NEW_STRING\|/g; s/\|LAST_UPDATE_DT/\|CURR_UPDATE_DT/g }'

In above command string, I am trying to replace few Strings:

Server1> echo $SERVER_COMMAND | sed -e "s|SEARCHFIELDAPP|ALLOWSERVICE|;s|SEARCHFIELDDB|DataDup|;s|SEARCHFIELDUNM|mogga|;s|OLD_STRING|mogga_dev_$%^|;s|NEW_STRING|mogga&*%^$|;s|LAST_UPDATE_DT|09-21-2012:11:09:41|;s|CURR_UPDATE_DT|09-21-2012:11:18:17|;"

When I do that I am getting an output as shown below:

perl -F'\|' -i -lape 'if ($F[0] eq "ALLOWSERVICE" && $F[1] eq "DataDup" && $F[6] eq "mogga" ) { s/\|mogga_dev_$%^\|/\|moggaNEW_STRING*%^$\|/g; s/\|09-21-2012:11:09:41/\|09-21-2012:11:18:17/g }'

Problem here is with substitution s|NEW_STRING|mogga&*%^$|; . I was expecting NEW_STRING to be replaced with mogga&*%^$ but it is getting replaced with moggaNEW_STRING*%^$.

Here & is getting interpreted as NEW_STRING. When I escape &, it works fine. But I get this string from a parent script which i use in child script dynamically. I am looking for some way to successfully substitute string as it is, without escaping any character. Escaping will make me to modify incoming string with escape character which i want to avoid.

I tried the same by using perl one liner for substitution. But here also i am facing similar issue with special character.

Server1> echo $SERVER_COMMAND |  perl -p -i -e 's|SEARCHFIELDAPP|ALLOWSERVICE|;s|SEARCHFIELDDB|DataDup|;s|SEARCHFIELDUNM|mogga|;s|OLD_STRING|mogga_dev_$%^|;s|NEW_STRING|mogga&*%^|;s|LAST_UPDATE_DT|09-21-2012:11:09:41|;s|CURR_UPDATE_DT|09-21-2012:11:18:17|;'

Below is how it looks after substitution:

perl -F'\|' -i -lape 'if ($F[0] eq "ALLOWSERVICE" && $F[1] eq "DataDup" && $F[6] eq "mogga" ) { s/\|mogga_dev_0^\|/\|mogga&*%^\|/g; s/\|09-21-2012:11:09:41/\|09-21-2012:11:18:17/g }'

I am seeing string OLD_STRING which was suppose to get replaced with string mogga_dev_$%^ got replaced with mogga_dev_0^. Here $% is getting converted to 0

In this case as well i get what expect by escaping problematic characters.

Can you please let me know how to avoid this interpretation apart from escaping special characters?

Thanks


After William’s inputs I was able to achieve below without errors:

perl -F'\|' -i -lape 'if ($F[0] eq "ALLOWSERVICE" && $F[1] eq "DataDup" && $F[6] eq "mogga" ) { s/\|mogga_dev_!$%^\|/\|mogga!&*%^$@*&^%\|/g; s/\|09-21-2012:11:09:41/\|09-21-2012:11:18:17/g }'

But when I execute this on a file for substitution. I am not seeing string mogga_dev_!$%^ is not getting replaced with mogga!&%^$@&^% in the file.

Since special characters are not escaped, I think replacement is not happening. But I dont want to use escaping to escape special characters. Can you please help me with some other method in replacing string with special characters in file?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:08:14+00:00Added an answer on June 11, 2026 at 6:08 pm

    In perl, a simple way to avoid any interpolation is to use ' as the delimiter. eg.

    perl -pe "s'OLD_STRING'mogga_dev_\$%^'"
    

    In this case, we must escape the $ to get the string mogga_dev_$%^ into perl, but perhaps this can help you. It’s not clear to me exactly which strings you are trying to avoid adding escape characters to, and if you have the string mogga_dev_$%^ in a string NEW_STRING, you can do:

    perl -pe "s'"$OLD_STRING"'"$NEW_STRING"'"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to match a line and use sed command to substitute it.
I am trying to create a program which will substitute a character in place
I've been trying to code a Perl script to substitute some text on all
I've been trying to substitute for a deprecated method which was in the OMS
I'm trying to change values in my application.properties file and I'm running into issues
Possible Duplicate: Substitute multiple whitespace with single whitespace in Python trying to figure out
I am trying to substitute <br> to \n but do not find how use
I am trying to learn OpenGL on the iPhone using the Super Bible but
I'm trying to remove a specific word from a certain string using the function
I am trying to match patterns in perl and need some help. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.