用子字符串替换后续分隔符之间的字符串

时间:2017-10-17 18:15:50

标签: java regex delimiter aix delimited-text

我有一个17GB的管道分隔的.txt文件,需要将第32个和第33个管道之间的任何超过10个字符的字符串替换为前10个字符,以便在不打开文件的情况下填充数据库列在崇高文本中;所以它需要通过Java或AIX-BASH来完成。在regex101.com我试图实现以下帖子中提出的想法:

RegEx: Match nth occurence

但它并没有将匹配的模式仅限制在我的替换字符串中。

示例输入:

|12210|IA||15||i956-743||||||l.4073||||a5015b3ed||l.464939|IC|||06 06:18:17||wireered||ENTITY|wirvered|2||||NoPodfoundorpoddoesnothaveedgetob-rd=l.415.63Z|REY||||RY|REY||

预期输出:

...|NoPodfundddorpoddoesnot...|...更改为...|NoPodfundd|...

替换/截断后的完整输出字符串:

|12210|IA||15||i956-743||||||l.4073||||a5015b3ed||l.464939|IC|||06 06:18:17||wireered||ENTITY|wirvered|2||||NoPodfundd|REY||||RY|REY||

尝试正则表达式匹配:

^(?:[^|]*\|){32}[^|]+\|匹配从开头到第33 |的所有内容,所以|12210.......l.415.63Z|,但我希望它只匹配管道32和33之间的字符串,特别是{{1} },用于替换目的。

更新1; 17年10月18日:

使用NoPodfoundorpoddoesnothaveedgetob-rd=l.415.63Z进行

(^(?:[^|]*\|){32}[^|]{0,10})([^|]*)(\|.*$)组捕获替换可提供所需的结果。但是这场比赛必须有一个缺陷,因为它似乎捕获了一个非捕获组\1\3

更新2; 17年10月19日:

在PUTTY命令行中尝试了以下命令,但它不编辑文件:

(?:[^|]*\|)

https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html建议

cat subStrTest.txt awk 'BEGIN{FS=OFS="|"}{$33=substr($33,1,10)} 1' subStrTest.txt

是有效的语法,至少对于string = substr(string,startIndex,numOfCharacters),但我不知道该作业是否

gawk

对于$33=substr($33,1,10)引用的字符串有效,如$中的$33

2 个答案:

答案 0 :(得分:0)

您可以制作匹配组并将其替换为其他数据 ^(?:[^|]*\|){32}([^|]+)\|

答案 1 :(得分:0)

See regex in use here

<强>正则表达式

Traceback (most recent call last):
  File "main.py", line 310, in <module>
    launch()
  File "main.py", line 26, in launch
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
  File "C:\Python\lib\site-packages\appium\webdriver\webdriver.py", line 36, in __init__
    super(WebDriver, self).__init__(command_executor, desired_capabilities, browser_profile, proxy, keep_alive)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Error occured while starting App. Original error: Error executing adbExec. Original error: 'Command 'C\:\\AndroidSDK\\platform-tools\\adb.exe -P 5037 -s GCNPCX031761JAV shell am start -W -n com.rockfordfosgate.perfecttune/com.rockfordfosgate.perfecttune.activity.HomeActivity -S' exited with code 1'; Stderr: 'java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.rockfordfosgate.perfecttune/.activity.HomeActivity } from null (pid=10837, uid=2000) not exported from uid 10127
        at android.os.Parcel.readException(Parcel.java:1683)
        at android.os.Parcel.readException(Parcel.java:1636)
        at android.app.ActivityManagerProxy.startActivityAndWait(ActivityManagerNative.java:3352)
        at com.android.commands.am.Am.runStart(Am.java:630)
        at com.android.commands.am.Am.onRun(Am.java:388)
        at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
        at com.android.commands.am.Am.main(Am.java:121)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:294)'; Code: '1'

<强>替换

^((?:[^|]*\|){32})(([^|]{0,10})[^|]*)(?=\|)