获取sed提取两点之间的文本,但在第二次出现字符串时停止 使用sed 使用awk

如何解决获取sed提取两点之间的文本,但在第二次出现字符串时停止 使用sed 使用awk

我有一个文本文件,其中包含许多电源数据。我要提取一个看起来像这样的部分:

 scan 170110 fission power at tpd 220635.7   total power  107.127

                     Triad3PC-ScanPower v1.1.1.1   ld=2007-03-27

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

 34                                               ...                                            34
 33                                   ...     ...     ...     ...                                33
 32                               ...     ...     ...     ...     ...                            32
 31                          ...      ...     ...     ...     ...     ...                        31
 30                      ...      ...     ...     ...     ...     ...     ...                    30
 29                  ...     ...    1.280   1.393   1.232     ...     ...     ...                29
 28                      ...    0.597   1.390     ...   1.289     ...     ...                    28
 27                  ...   0.534    1.113     ...     ...     ...     ...     ...                27
 26              ...     ...    1.536     ...     ...   0.706     ...     ...     ...            26
 25          ...     ...   1.207    1.128     ...     ...   0.784     ...     ...     ...        25
 24              ...     ...      ...   0.791   0.831     ...     ...   0.733     ...            24
 23          ...     ...     ...      ...     ...   1.069     ...     ...   1.226     ...        23
 22              ...     ...      ...   0.945   1.204   0.841     ...   0.687     ...            22
 21          ...     ...   1.352      ...   0.826     ...     ...   1.134   1.383     ...        21
 20      ...     ...     ...    2.246     ...   1.639   1.316     ...     ...     ...     ...    20
 19          ...   1.214   0.953    1.551   1.434     ...   1.229   1.048     ...     ...        19
 18      ...     ...   1.256    0.824     ...     ...   1.151   1.132     ...   1.552     ...    18
 17          ...   0.701   1.311      ...   1.796   1.492     ...   2.365   1.101     ...        17
 16      ...     ...     ...      ...     ...     ...     ...   0.970   1.042   1.467     ...    16
 15          ...   1.234     ...    0.863   1.574     ...   1.050   0.923     ...     ...        15
 14      ...     ...     ...      ...     ...   1.642   1.571     ...   1.090   1.473     ...    14
 13          ...     ...   1.062    1.266   1.679   1.599   0.814     ...   1.506     ...        13
 12      ...     ...   1.430      ...     ...     ...     ...     ...     ...     ...     ...    12
 11          ...     ...   0.978      ...   1.342   1.375     ...     ...   1.318     ...        11
 10              ...     ...      ...   0.963     ...   1.066     ...   0.783   0.480            10
  9          ...   1.056     ...    1.291     ...     ...   0.595     ...   0.145     ...         9
  8              ...   1.230      ...     ...   0.760     ...     ...     ...     ...             8
  7          ...     ...   1.313      ...   0.907     ...   0.762   1.534     ...     ...         7
  6              ...     ...    1.306     ...     ...     ...   1.292     ...     ...             6
  5                  ...     ...    1.012   0.818     ...     ...     ...     ...                 5
  4                      ...      ...   1.252     ...   1.299     ...     ...                     4
  3                  ...     ...      ...   1.221   1.145   1.109     ...     ...                 3
  2                      ...      ...     ...     ...     ...     ...     ...                     2
  1                          ...      ...     ...     ...     ...     ...                         1
  0                               ...     ...     ...     ...     ...                             0
 -1                                   ...     ...     ...     ...                                -1
 -2                                               ...                                            -2

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

我的sed命令是:

sed -n  '/fission power at tpd/,/ y   z   a   b   c/ { p; }' s.171012.so.power.info

但是sed最终停止在“ y z a b c”的第一个实例处

     scan 171012 fission power at tpd 239802.1   total power  109.976

                     Triad3PC-ScanPower v1.1.1.1   ld=2007-03-27

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t

我希望它在地图底部的第二个实例处停止。我如何告诉它做到这一点?

解决方法

使用sed

尝试:

sed -n  '/fission power at tpd/,/ y   z   a   b   c/{/ y   z   a   b   c/!p;}; / y   z   a   b   c/,/ y   z   a   b   c/ { p; }' s.171012.so.power.info

为便于阅读,请考虑以下测试文件:

$ cat file.info
0
begin
1
head
2
3
head
4

我们的命令产生以下输出:

$ sed -n '/begin/,/head/{/head/!p;}; /head/,/head/p' file.info
begin
1
head
2
3
head

/begin/,/head/{/head/!p;}从包含begin的行开始打印,直到但不包括包含head的第一行。 /head/,/head/p从包含head的行打印到包含head的下一行。

使用awk

由于awk了解算术,我们可以计算包含head的行的出现次数:

$ awk '/begin/{f=1}; f && g<2; f && /head/{g++}' file.info
begin
1
head
2
3
head

与sed一样,awk一次处理输入文件一行。在这里,当我们到达包含f的行时,将awk变量begin设置为1(true)。如果f为true并且g小于2(表示少于两次head),我们将打印该行。如果f为true并且行包含head,我们将递增g

,

这可能对您有用(GNU sed):

sed -n '/fission power at tpd/{:a;N;s/^.*y   z   a   b   c.*/&/M2p;Ta}' file

使用-n选项关闭显式打印,即更像grep。

将模式空间中的行从包含fission power at tpd的行收集到另一行,这是包含y z a b c的行的第二次出现。

该解决方案在多行模式下使用替代命令,并在出现两次时替换自己匹配的行。成功替换命令将设置一个内部标志,如果未设置内部标志,则T命令将程序流传输到循环标签。因此,循环将在模式空间中收集线,在进行匹配时将其打印并退出循环。

, 如果对单个字符串进行s / old / new操作,

sed是最好的工具。这不是您要执行的操作,因此会选择一个糟糕的选择来尝试将其用于您的应用程序。在每个UNIX框的任何外壳中使用任何awk:

awk '/fission power at tpd/{f=1} f{print; if ((/y   z   a   b   c/) && (++c == 2)) exit}' file
,

解决方案是缓冲读取的行,直到遇到停止模式(在这种情况下应将其输出)或直到遇到文件末尾(在这种情况下,自上一个停止模式以来的已缓冲的行都将被丢弃) 。以下解决方案使用~作为保留空间内“已缓冲要输出”行和“已缓冲但不打算输出”行之间的分隔符:

start="fission power at tpd"
stop=" y   z   a   b   c"
sed -n '
    # Filter the starting pattern 
    /'"$start"'/,${
        # Add the separator to and hold the line
        s/$/~/; h;
        # foreach line
        :again; n;
            # We hold the line unconditionally,cause its after ~ separator
            H;
            # If its stopping pattern
            /'"$stop"'/{
                # Move the ~ separator to the end of hold buffer
                x;s/~//;s/$/~/;x;
            };
            # If its end of file
            ${
                # output from the hold buffe only the part before ~ separator
                x;s/~.*//;p;q;
            };
            b again;
}' input

还有一个班轮:

sed -n '/'"$start"'/,${ s/$/~/; h; :again; n; H; /'"$stop"'/{x;s/~//;s/$/~/;x;}; ${ x;s/~.*//;p;q; }; b again; }' input

通过在模式缓冲区上充当“不输出行”缓冲区,您可以在容纳空间内没有分隔符的情况下执行此操作。这会导致stop模式与整行不匹配,因此在编写模式时必须记住不要使用^锚点,而不能使用\n或更多\(^\|\n\)[^\n]*some_pattern[^\n]*$

sed -n '
    # Filter from the starting pattern to the of file
    /'"$start"'/,${
        # Hold the starting line and read next line
        h; n;
        # Foreach line
        :again; {
            # If stopping pattern is found
            /'"$stop"'/{
                # Add everything up until now to hold space
                H;
                # Now we could be potentially at end of file here - check it before reading next line
                $!{
                    # Clear pattern space,read next line and restart
                    n;
                    b again;
                }
            };
            # If end of file
            ${
                # print the content of hold space and quit
                x;p;q;
            };
            # Add next line to pattern space
            N;
        }; b again;
}' input

还有一个班轮:

sed -n '/'"$start"'/,${h; n; :again; /'"$stop"'/{ H; $!{ n; b again; }; }; ${ x;p;q; }; N; b again; }' input

Tested on repl

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-