Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: replace string from Command Line

by Fletch (Bishop)
on Oct 13, 2020 at 19:06 UTC ( [id://11122786]=note: print w/replies, xml ) Need Help??


in reply to replace string from Command Line

Most likely guess (since you don't show sample data which you're expecting to match against . . .) it's because you have unquoted regex metacharacters (namely parens) which mean that the LHS of your s/// isn't able to match what you're trying to match. Unfortunately backquoting all of those from a shell prompt is going to get ugly.

Show the text you're trying to change (a before and after will help) and specify what OS you're using (shell quoting on *NIX will differ from CMD or PowerShell) and you might be able to get a more specific answer.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: replace string from Command Line (updated)
by AnomalousMonk (Archbishop) on Oct 13, 2020 at 19:30 UTC

    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:  <%-{-{-{-<

      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:  <%-{-{-{-<

      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://11122786]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found