替换文件中第一个括号前的所有连字符

时间:2017-01-19 14:26:23

标签: regex perl sed pattern-matching

我正在尝试使用sed或perl来替换第一个实例之前文件中出现的所有连字符;

到目前为止,我有以下内容替换了我的文件中的所有连字符,但我不能让它只匹配“);”的第一个实例。

perl -pi -e 's/[^.*);]?-/_/' 00-90.sql

更多信息:

我正在尝试从已生成的create table SQL中删除无效连字符,我们有1k +文件可以通过,但我用它替换了所有连字符。

要清理的文字示例:

CREATE TABLE e_00_90 (
Last_Name nvarchar(50),
  Initials nvarchar(3),
  Company_Division nvarchar(50),
  Status nvarchar(12),
  Ckeyword5 nvarchar(15),
  Value_of_Contract_-_Overh decimal(20,2)
);
insert into export_00_90 values ('Sample-One', 'R', 'Div 1', 'Expired', 'ANONYTR', 5000);
insert into export_00_90 values ('Sample Two', 'R', 'Div_2', 'Expired', 'WISHBONE', 13000);

我只需要Create Table块来删除连字符。

4 个答案:

答案 0 :(得分:0)

(?:(?!PAT).)*PAT[^CHAR]CHAR,所以

s/\G (?: (?! \); | - ) . )* \K - /_/xsg

替代:

s/\G (?: (?! \); ) [^-] )* \K - /_/xg

最快:

s/\G (?: [^)-]++ | \)(?!;) )* \K - /_/xg

答案 1 :(得分:0)

使用GNU sed:

Executing: /home/ec2-user/jdk1.8.0_45/bin/java -Dretrolambda.inputDir=/tmp/build704717599398049525xxx/tmpClasses -Dretrolambda.classpath=/tmp/build704717599398049525xxx/tmpClasses:/tmp/temp8811672204922110315.jar -Dretrolambda.outputDir=/tmp/build704717599398049525xxx/tmpClasses_retrolamda -Dretrolambda.bytecodeVersion=49 -Dretrolambda.defaultMethods=true -jar /tmp/temp7216874003515660571.jar Retrolambda 2.0.3
Bytecode version: 49 (Java 5)
Default methods:  true
Input directory:  /tmp/build704717599398049525xxx/tmpClasses
Output directory: /tmp/build704717599398049525xxx/tmpClasses_retrolamda
Classpath:        /tmp/build704717599398049525xxx/tmpClasses:/tmp/temp8811672204922110315.jar
Executing: /home/ec2-user/javascript/apache-maven-3.2.5/bin/mvn -e clean package [INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for mobi.mfactory:HelloParse:war:1.0
[WARNING] 'dependencies.dependency.systemPath' for ca.weblite:codename1-core:jar should not point at files within the project directory, ${basedir}/lib/CodenameOne.jar will be unresolvable by dependent projects @ line 24, column 25
[WARNING] 'dependencies.dependency.systemPath' for ca.weblite:codename1-factory:jar should not point at files within the project directory, ${basedir}/lib/Factory.jar will be unresolvable by dependent projects @ line 32, column 25
[WARNING] 'dependencies.dependency.systemPath' for ca.weblite:codename1-core-hello:jar should not point at files within the project directory, ${basedir}/lib/app.jar will be unresolvable by dependent projects @ line 40, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloParse 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloParse ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloParse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloParse ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 59 source files to /tmp/build704717599398049525xxx/target/classes
[WARNING] /tmp/build704717599398049525xxx/src/main/java/com/codename1/impl/html5/Stub.java: Some input files use or override a deprecated API.
[WARNING] /tmp/build704717599398049525xxx/src/main/java/com/codename1/impl/html5/Stub.java: Recompile with -Xlint:deprecation for details.
[WARNING] /tmp/build704717599398049525xxx/src/main/java/com/codename1/teavm/io/BlobUtil.java: Some input files use unchecked or unsafe operations.
[WARNING] /tmp/build704717599398049525xxx/src/main/java/com/codename1/teavm/io/BlobUtil.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- teavm-maven-plugin:1.0.0-cn1-007:compile (web-client) @ HelloParse ---
[INFO] Preparing classpath for JavaScript generation
[INFO] Using the following classpath for JavaScript generation: /tmp/build704717599398049525xxx/lib/CodenameOne.jar:/tmp/build704717599398049525xxx/lib/Factory.jar:/tmp/build704717599398049525xxx/lib/app.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-classlib/1.0.0-cn1-007/teavm-classlib-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-platform/1.0.0-cn1-007/teavm-platform-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-core/1.0.0-cn1-007/teavm-core-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-interop/1.0.0-cn1-007/teavm-interop-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/home/ec2-user/.m2/repository/com/carrotsearch/hppc/0.6.1/hppc-0.6.1.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-jso-apis/1.0.0-cn1-007/teavm-jso-apis-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-jso-impl/1.0.0-cn1-007/teavm-jso-impl-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/org/mozilla/rhino/1.7.7/rhino-1.7.7.jar:/home/ec2-user/.m2/repository/org/ow2/asm/asm-debug-all/5.0.4/asm-debug-all-5.0.4.jar:/home/ec2-user/.m2/repository/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar:/home/ec2-user/.m2/repository/com/jcraft/jzlib/1.1.3/jzlib-1.1.3.jar:/home/ec2-user/.m2/repository/joda-time/joda-time/2.7/joda-time-2.7.jar:/home/ec2-user/.m2/repository/org/teavm/teavm-jso/1.0.0-cn1-007/teavm-jso-1.0.0-cn1-007.jar:/home/ec2-user/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/home/ec2-user/.m2/repository/org/mitre/dsmiley/httpproxy/smiley-http-proxy-servlet/1.6/smiley-http-proxy-servlet-1.6.jar:/home/ec2-user/.m2/repository/org/apache/httpcomponents/httpclient/4.3.4/httpclient-4.3.4.jar:/home/ec2-user/.m2/repository/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar:/home/ec2-user/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/home/ec2-user/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/tmp/build704717599398049525xxx/target/classes
[INFO] Building JavaScript file
[INFO] Output file built with errors
[ERROR] Method java.lang.Class.getCanonicalName()Ljava/lang/String; was not found
    at com.parse4cn1.encode.ParseEncoder.encode(ParseEncoder.java:173)
    at com.parse4cn1.operation.SetFieldOperation.encode(SetFieldOperation.java:45)
    at com.parse4cn1.ParseObject.getParseData(ParseObject.java:781)
    at com.parse4cn1.ParseObject.performSave(ParseObject.java:812)
    at com.parse4cn1.ParseObject.save(ParseObject.java:682)
    at mobi.mfactory.helloparse.HelloParse.createTask(HelloParse.java:55)
    at mobi.mfactory.helloparse.HelloParse.start(HelloParse.java:242)
    at com.codename1.impl.html5.Stub.run(Stub.java:87)
    at com.codename1.ui.Display.callSerially(Display.java:772)
    at com.codename1.impl.html5.Stub.main(Stub.java:75)
[INFO] Debug information successfully written
[INFO] Source maps successfully written
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.183 s
[INFO] Finished at: 2017-01-19T14:40:18+00:00
[INFO] Final Memory: 28M/893M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.teavm:teavm-maven-plugin:1.0.0-cn1-007:compile (web-client) on project HelloParse: Build error -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.teavm:teavm-maven-plugin:1.0.0-cn1-007:compile (web-client) on project HelloParse: Build error
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Build error
    at org.teavm.maven.TeaVMCompileMojo.execute(TeaVMCompileMojo.java:112)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 19 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Currently 'stopOnErrors' is on.  With this enabled, even build warnings will cause the build to fail. 
Try adding the 'javascript.stopOnErrors=false' build hint and see if it builds.  
WARNING: switching stopOnErrors off may result in runtime errors, if the warnings actually fall in the execution path of the app. 

答案 2 :(得分:0)

[编辑]
用awk:

awk -v RS=');' -v ORS=');' '/^CREATE TABLE/{gsub("-","_")}1'

使用perl:

perl -pe'BEGIN{$/=");"} /^CREATE TABLE/&&y/-/_/'

使用sed:

sed '/^CREATE TABLE/{:;/);/!{N;b};y/-/_/;}'

awk和perl方式相同,它们使用记录分隔符);,检查这个是否以“CREATE TABLE”开头并将每个连字符转换为下划线。

sed版本差别不大,只是你需要自己构建文本块,将每一行追加到);(请注意,这种方式假设在同一行中的);之后没有其他代码)

{
  :  # define an empty label
  /);/! # if ); doesn't match
  {
    N # append a newline to the pattern space
    b # go to the label
  }
  y/-/_/ # translate
}

[OLD ANSWER:仅按行工作]
用sed:

sed -E ':;s/^(([^)-]|\)[^;-])*\)*)-/\1_/;t'

或perl:

perl -pe's/.*?\);|.+/$&=~y|-|_|r/e'

(这种方式匹配所有字符串直到第一个);或字符串结尾,然后匹配中的每个连字符都使用y///转换为下划线

答案 3 :(得分:0)

我不会尝试使用魔法正则表达式解决它,而是:

#!/usr/bin/env perl
use strict;
use warnings;

while ( <DATA> ) {
   if ( m/CREATE TABLE/ .. /\);/ ) { 
      s/-/_/g;
   }
   print;
}


__DATA__
CREATE TABLE e_00_90 (
Last_Name nvarchar(50),
  Initials nvarchar(3),
  Company_Division nvarchar(50),
  Status nvarchar(12),
  Ckeyword5 nvarchar(15),
  Value_of_Contract_-_Overh decimal(20,2)
);
insert into export_00_90 values ('Sample-One', 'R', 'Div 1', 'Expired', 'ANONYTR', 5000);
insert into export_00_90 values ('Sample Two', 'R', 'Div_2', 'Expired', 'WISHBONE', 13000);

范围运算符可让您测试是否在两个分隔符之间,并且仅在那里应用表达式。

作为一个班轮,这将是:

perl -pi -e 'm/CREATE TABLE/ .. /\);/ && s/-/_/g' 00-90.sql