使用awk将两个模式之间的行输出到不同的输出文件中 [英] Using awk to output lines between two patterns into different output files

查看:111
本文介绍了使用awk将两个模式之间的行输出到不同的输出文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中有许多以"MODEL"开头的开始标记行",以及许多以"ENDMDL"开头的"end token line"结尾的行.

I have a file with many "start token lines" that starts with the word "MODEL", and many lines that end with an "end token line" that starts with the word "ENDMDL".

我想解析该文件,以便它将所有从开始令牌行"开始到结束令牌行"结束的行都捕获到一个新的输出文件中.

I would like to parse the file so that it grabs all lines starting from the "start token line" and ending at the "end token line" into a new output file.

换句话说,如果我在一个包含100个开始标记行"和结束标记行"的文件上运行此文件,我想产生100个文件.

In other words, if I ran this on a file with 100 of these "start token line" and "end token line", I would like to produce 100 files.

我有一个awk命令正在工作:

I have an awk command working:

 awk '/MODEL/ {flag=1;next} /ENDMDL/{flag=0} flag {print}' 1KZS.pdb > TEST

但是,此命令仅将MODEL-> ENDMDL之间的所有行打印到相同输出文件中.但是我希望每个MODEL-> ENDMDL都输出到一个 new 输出文件中.

However, this command just prints all lines between MODEL-->ENDMDL into the same output file. But I would like each MODEL-->ENDMDL to output in a new output file.

如何调整我的awk命令来完成此任务?

How can my awk command be tweaked to accomplish this?

推荐答案

这可以解决问题:

$ awk '/MODEL/{f=1;s="FILE"++i;next}/ENDMDL/{f=0;close(s)}f{print > s}' 1KZS.pdb

这篇关于使用awk将两个模式之间的行输出到不同的输出文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆