使用 sed 找到负+正模式时插入新行

使用 sed 找到负+正模式时插入新行

有一堆 txt 文件需要正确清理和解析。我需要在模式“SP”时换行。已找到,但在模式“ASSERT.SP.”时未找到。被发现。

样本内容:

    SP. 247 for specific issues no really solved
    ASSERT. SP. 4532 no so valuable it depends on primary conditions
    At first location in London City SP. 3901 must be applied
    ASSERT. SP. 23245 must be followed by procedure SP. 8236 in all steps
    Special tools are needed for SP. 9734 to be accomplished

期望的结果:

        SP. 247 for specific issues no really solved
        ASSERT. SP. 4532 no so valuable it depends on primary conditions
        At first location in London City 
        SP. 3901 must be applied
        ASSERT. SP. 23245 must be followed by procedure 
        SP. 8236 in all steps
        Special tools are needed for 
        SP. 9734 to be accomplished

我的第一个方法是使用正则表达式找出是否“SP。”不是前面加一个点,然后替换为“换行符 + SP”。但到目前为止还没有成功。

sed -r 's/([^\.] )(SP\. )/\nSP\. /g' 

答案1

sed需要对OP中发布的解决方案进行一些调整

sed -r 's/([^.] )(SP\. )/\1\n\2/g'

问题s/([^\.] )(SP\. )/\nSP\. /g是它会丢弃([^\.] ).也不.需要在内部[]以及替换部分中转义

答案2

通常相同的任务可以通过用稀有符号替换未挂起的部分然后将其返回来解决

sed '
    s/\(^\s*\|ASSERT\. \)SP\./\1\a/g
    s/SP\./\n&/g
    s/\a/SP./g
    '

答案3

我会这样做

sed -r '
    # for lines without "ASSERT.", add a newline before "SP."
    # unless it is only preceded by whitespace
    /ASSERT\./! s/^(.*[^[:blank:]].*)(SP\.)/\1\n\2/

    # for lines containing "ASSERT.", add a newline before the last "SP."
    s/^(.*ASSERT\..*SP\..*)(SP\..*)/\1\n\2/
' file

相关内容