Conditional removing of text using sed

huangapple go评论58阅读模式
英文:

Conditional removing of text using sed

问题

以下是您要的翻译结果:

我有这个文件:

HEADER2023053112460600000004000000000.000002023053112364820230531123707
20230704225151KKKKKKKKK         9C28F1EB01341234567890123   0000472MO000000000.00000N82
20230703215437KKKKKKKKK         992CEDE00134112233445       0000022MO000000000.00000NSL
20230704111018KKKKKKKKK         A4C0B8000127987654321       0000532MO000000000.00000NSL

我需要删除第二列的第11和第12个字符,如果它们分别匹配3和4。

我能想到的最好的方法是这样的:

sed -E 's/^([^[:blank:]]+[[:blank:]]+)([^[:blank:]]{10})(34)([^[:blank:]]+)[[:blank:]]+/ /' 文件名

它几乎能够得到期望的结果。问题是第三列和第四列之间的间距:被替换的行干扰了原始的空格数量,我需要保持完全相同的空格。

希望对您有所帮助。

英文:

I have this file:

HEADER2023053112460600000004000000000.000002023053112364820230531123707
20230704225151KKKKKKKKK         9C28F1EB01341234567890123   0000472MO000000000.00000N82
20230703215437KKKKKKKKK         992CEDE00134112233445       0000022MO000000000.00000NSL
20230704111018KKKKKKKKK         A4C0B8000127987654321       0000532MO000000000.00000NSL

And I need to remove the 11th and 12th characters on the second column if they both match 3 and 4 respectively.

The best I could came up with is this:

sed -E 's/^([^[:blank:]]+[[:blank:]]+)([^[:blank:]]{10})(34)([^[:blank:]]+)[[:blank:]]+/\1\2\4 /' file

HEADER2023053112460600000004000000000.000002023053112364820230531123707
20230704225151KKKKKKKKK         9C28F1EB011234567890123 0000472MO000000000.00000N82
20230703215437KKKKKKKKK         992CEDE001112233445 0000022MO000000000.00000NSL
20230704111018KKKKKKKKK         A4C0B8000127987654321       0000532MO000000000.00000NSL

Which almost gets the desired result. Problem is the spacing between the third and fourth column: the lines that got replaced messed with the original number of spaces and I need it to be the exact same thing.

Any help would be most welcome.

答案1

得分: 2

根据您提供的输入示例,输出如下:

HEADER2023053112460600000004000000000.000002023053112364820230531123707
20230704225151KKKKKKKKK         9C28F1EB011234567890123     0000472MO000000000.00000N82
20230703215437KKKKKKKKK         992CEDE001112233445         0000022MO000000000.00000NSL
20230704111018KKKKKKKKK         A4C0B8000127987654321       0000532MO000000000.00000NSL

希望对您有所帮助。

英文:

Suggestion :

sed -e '1b; s/^\(\S\+\s\+\w\{10\}\)34\(\w\+\)/  /' INPUTFILE

Based on the input sample your provided, the output is:

HEADER2023053112460600000004000000000.000002023053112364820230531123707
20230704225151KKKKKKKKK         9C28F1EB011234567890123     0000472MO000000000.00000N82
20230703215437KKKKKKKKK         992CEDE001112233445         0000022MO000000000.00000NSL
20230704111018KKKKKKKKK         A4C0B8000127987654321       0000532MO000000000.00000NSL

Hope that helps.

huangapple
  • 本文由 发表于 2023年7月6日 18:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定