简体   繁体   English

使用sed / awk命令找到模式后,修改文本文件的内容

[英]Modify the contents of a text file after a pattern has been found using sed/awk commands

I tried to come up with a Tcl script, but I'm looking for something in sed/awk commands to make this faster. 我试图提出一个Tcl脚本,但是我正在sed / awk命令中寻找一些东西来使它更快。

The file contains the following: 该文件包含以下内容:

*Other text*
.....
.....
.....
SECTION LABELS {
lab M1 0 0 0 0 3 1 bot
lab M2 0 0 0 0 3 1 top
} SECTION LABELS

*Other text*
......
......
......
SECTION LABELS {
lab M1:NET 5207 12261 5207 12261 0 2 A
lab M2:NET 6880 5370 6880 5370 0 2 B
lab M1:NET 3454 5386 3454 5386 0 2 alpha
lab M2:NET 3454 5386 3454 5386 0 2 beta
} SECTION LABELS

I'm interested only in the lines containing within "SECTION LABELS". 我只对“ SECTION LABELS”中包含的行感兴趣。 I'd like to: 我想:

  • Change M1:NET to M1 and M2:NET to M2 将M1:NET更改为M1,将M2:NET更改为M2
  • If the last element is A or B, change the number before A or B from 2 to 3 如果最后一个元素是A或B,则将A或B之前的数字从2更改为3
  • If the last element is anything else, change the number (last but one) from 2 to 6 如果最后一个元素是其他任何元素,请将数字(最后一个,但一个除外)从2更改为6
  • If the numbers after lab M1:NET or lab M1 are 0 0 0 0, then I do not want to change anything. 如果实验室M1:NET或实验室M1之后的数字为0 0 0 0,那么我不想更改任何内容。
  • Rest of the contents of the file remain the same 文件的其余内容保持不变

So the output looks like: 所以输出看起来像:

*Other text*
.....
.....
.....
SECTION LABELS {
lab M1 0 0 0 0 3 1 bot
lab M2 0 0 0 0 3 1 top
} SECTION LABELS

*Other text*
......
......
......
SECTION LABELS {
lab M1 5207 12261 5207 12261 0 3 A
lab M2 6880 5370 6880 5370 0 3 B
lab M1 3454 5386 3454 5386 0 6 alpha
lab M2 3454 5386 3454 5386 0 6 beta
} SECTION LABELS

Here is some to get you started: 以下是一些入门指南:

awk '/SECTION LABELS/ {f=!f} f && /NET/ && !/lab M1(:NET)* 0 0 0 0/ {split($2,a,":");$2=a[1];if ($NF~/\<[A|B]\>/) $(NF-1)=3; else $(NF-1)=6}1' t
*Other text*
.....
.....
.....
SECTION LABELS {
lab M1 0 0 0 0 3 1 bot
lab M2 0 0 0 0 3 1 top
} SECTION LABELS

*Other text*
......
......
......
SECTION LABELS {
lab M1 5207 12261 5207 12261 0 3 A
lab M2 6880 5370 6880 5370 0 3 B
lab M1 3454 5386 3454 5386 0 6 alpha
lab M2 3454 5386 3454 5386 0 6 beta
} SECTION LABELS

You can use the range as the pattern and then do operation on texts in between (inclusive). 您可以将范围用作模式,然后对介于两者之间(包括两端)的文本进行操作。 for your input: 供您输入:

awk '/^SECTION LABELS {/,/} SECTION LABELS/{
    sub(":NET","")
    if ($8 && !/M1 0 0 0 0/) $8 = ($NF ~ /^[AB]$/) ? 3 : 6 
}1' file.txt

Note: (1) you will need to adjust the 'if' section if there are more than 7 words in 'SECTION LABELS' (I supposed this is just a sample). 注意:(1)如果“ SECTION LABELS”中的单词超过7个,则需要调整“ if”部分(我想这只是一个示例)。 you can change '$8' to '$9' or $9 != "" etc to skip the header and trailer. 您可以将'$ 8'更改为'$ 9'或$ 9!=“”等,以跳过标题和尾部。

(2) If A, B are string instead of chars, you will need to change the regex from ^[AB]$ to ^(A|B)$ (2)如果A,B是字符串而不是字符,则需要将正则表达式从^ [AB] $更改为^(A | B)$

(3) If you also want to keep M2 0 0 0 0 as is, then change !/M1 0 0 0 0/ to !/\\<0 0 0 0\\>/ where '\\<' and '\\>' are word boundaries in regex. (3)如果还想保持M2 0 0 0 0不变,则将!/ M1 0 0 0 0 /更改为!/ \\ <0 0 0 0 \\> /,其中'\\ <'和'\\>'为正则表达式中的单词边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM