How to write a Perl script to replace duplicate words
How can I formulate a command-l开发者_如何转开发ine Perl script to replace all duplicate words with chosen aliases. Currently I have a T-SQL query with about 1000 lines. But because this query doesn't use any aliases, it is difficult to understand and visualize. So I go through it by hand and convert table names to aliases.
But it would be more efficient to have a Perl script do this for me. Can you either advise how I might do this via Perl or else if you know of a SQL tool that can do it instead?
Here is what I tried:
C:\STRAWB~1\perl\bin>perl -pi.orig -we 's/\bV_QXP_ALL_EXCEPTION\b/A/g' test.sql
Useless use of a constant (s/\bV_QXP_ALL_EXCEPTION\b/A/g) in void context at -e
line 1.
To replace a table name like business_interaction_shoptype_whatnot
with wtf
, do the following on your SQL file:
perl -pi.orig -we 's/\bbusiness_interaction_shoptype_whatnot\b/wtf/g' wtf.sql
This will mofify the input file in-place while storing a pristine copy in wtf.sql.orig
.
I take your question to mean you want a command-line to edit files "on the fly" with, and not a file that you can re-use when you need it.
The -i switch can be used to edit a file in place. The basic formula you are after is:
$ perl -i.extension -e CODE FILE
For example: perl -i.bak -e 's/find/replace/g' file.txt
Documentation here: http://perldoc.perl.org/perlrun.html
I would recommend not using a command-line though, especially if legibility is what you are after. I am not sure what kind of text you are replacing, so it is hard to say what the regexp should look like.
#!/bin/perl -i.bak
# Notice the "-i" switch
use strict;
use warnings;
while (<>) {
s/Case-Sensitive-text/CST/g;
s/case-insensitive-text/CIT/ig;
s/\[text inside brackets\]/no_brackets/g;
}
This file based script will do the same if called with script.pl myfile.txt
ETA:
C:\STRAWB~1\perl\bin>perl -pi.orig -we 's/\bV_QXP_ALL_EXCEPTION\b/A/g' test.sql
Useless use of a constant (s/\bV_QXP_ALL_EXCEPTION\b/A/g) in void context at -e
line 1.
Why are you messing around inside the perl binary folder? =)
In Windows, you use the double quote instead of single quote when doing command line stuff... for some reason.
精彩评论