Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: replace string from Command Line (updated)

by AnomalousMonk (Archbishop)
on Oct 13, 2020 at 19:30 UTC ( [id://11122789]=note: print w/replies, xml ) Need Help??


in reply to Re: replace string from Command Line
in thread replace string from Command Line

s/CAST(CAST(hs_date ...) AS CHAR(08)) as .../... whatever .../g
... backquoting all of those from a shell prompt is going to get ugly.
Interpolation control escapes (if that's the proper terminology) \Q \E to the rescue:
    s/\QCAST(CAST(hs_date ...) AS CHAR(08)) as ...\E/.../g
(but those long strings still look scary). Note that the spaces in the search string must match exactly, and the match will fail if the string in the text being searched is broken across two or more lines

Update: See Quote and Quote-like Operators (buried in the middle of the section) in perlop for a discussion of \l \u \L \U \F \Q \E escapes.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: replace string from Command Line (updated)
by pragovnj (Acolyte) on Oct 13, 2020 at 20:26 UTC

    I am trying to update my sql which was written for Teradata DB to be compatible to the DVT. DVT does not accept CAST(CAST(hs_date AS DATE FORMAT 'YYYYMMDD') AS CHAR(08)) as hs_date and only accepts FORMAT_DATE(hs_date, 'YYYYMMDD') as hs_date. I have more than 75 scripts which need to be replaced and compatible to DVT. I have identified these scripts and rather than open each script and replace the string, I thought I could do this from the command line

    ..... $sql = "select CAST(CAST(hs_date AS DATE format 'YYYYMMDD') AS CHAR(08 +)) as hs_date, " . "device, " . "thrshld as CPU_threshold, " . ...

      I have more than 75 scripts which need to be replaced and compatible to DVT. ... I thought I could do this from the command line
      This sounds like more than just a quick-and-dirty, one-off command-line task. Write yourself a script. Put all the filenames to be processed into a file and pass that filename to your script. Open each file, make a verified backup first, make all changes (and there will probably be others you need to do; these can easily be added to your script), then save the altered file. Only process files that are copies of files saved elsewhere!

      I'd like to provide a pseudo-script example, but I don't have time ATM. Maybe later if no one else steps in first. Just remember that the \Q...\E approach fails if there is any variation in spacing in the substrings to be altered or if they are broken over multiple lines. In that case, a broader approach is needed, but can easily be integrated into a general script.


      Give a man a fish:  <%-{-{-{-<

Re^3: replace string from Command Line (updated)
by pragovnj (Acolyte) on Oct 13, 2020 at 19:48 UTC

    This is what I tried but did not work

     perl -p -i.bak -e "s/\QCAST(CAST(hs_date AS DATE FORMAT 'YYYYMMDD') AS CHAR(08)) as hs_date\E/FORMAT_DATE(hs_date, 'YYYYMMDD') as hs_date/g;" HealthList

      What is your data?

      Win8 Strawberry 5.8.9.5 (32) Tue 10/13/2020 15:55:21 C:\@Work\Perl\monks\pragovnj >cat junque.txt foo bar CAST(CAST(hs_date AS DATE FORMAT 'YYYYMMDD') AS CHAR(08)) as h +s_date biz boz C:\@Work\Perl\monks\pragovnj >perl -p -i.bak -e "s/\QCAST(CAST(hs_date AS DATE FORMAT 'YYYYMMDD') A +S CHAR(08)) as hs_date\E/FORMAT_DATE(hs_date, 'YYYYMMDD') as hs_date/ +g;" junque.txt C:\@Work\Perl\monks\pragovnj >cat junque.txt foo bar FORMAT_DATE(hs_date, 'YYYYMMDD') as hs_date biz boz C:\@Work\Perl\monks\pragovnj >cat junque.txt.bak foo bar CAST(CAST(hs_date AS DATE FORMAT 'YYYYMMDD') AS CHAR(08)) as h +s_date biz boz
      (Sorry for any wrap-around. :) In general, "it doesn't work" is a very poor problem description.

      Update: Please see Short, Self-Contained, Correct Example for a discussion of good problem presentation practices.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-25 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found