简体   繁体   English

Linux脚本:如何使用匹配模式将文本拆分为不同的文件

[英]Linux script: how to split a text into different files with match pattern

For example: I have a text as following: 例如:我有如下文本:

Jul 11 xxxx  xxxx start xxxxx
....
....
....
Jul 11 xxxx  xxxx stop xxxxx
Jul 11 xxxx  xxxx start xxxxx
....
....
....
Jul 11 xxxx  xxxx stop xxxxx
....

now I want to split the above text file into different files based on "start" and "stop", like 现在我想根据“开始”和“停止”将上述文本文件拆分为不同的文件,例如

/***text1.txt******/
Jul 11 xxxx  xxxx start xxxxx
....
....
....
Jul 11 xxxx  xxxx stop xxxxx

/***text2.txt******/
Jul 11 xxxx  xxxx start xxxxx
....
....
....
Jul 11 xxxx  xxxx stop xxxxx

How can I do that? 我怎样才能做到这一点? Thanks. 谢谢。

This can make it: 这可以使它:

$ awk '{if ($0 ~ /start/) a++} {print >> "file"a}' file

Explanation 说明

  • {if ($0 ~ /start/) a++} looks for lines containing the word start . {if ($0 ~ /start/) a++}查找包含单词start If so, increments the variable a , which is 0 by default. 如果是这样,则将变量a递增,默认情况下为0
  • {print >> "file"}' prints $0 (that is, the whole line) to a file called "file" in the same directory. {print >> "file"}' $0 (即整行)打印到同一目录中名为“ file”的文件中。
  • {print >> "file"a} prints the line to a file called "file" + variable a , which happens to be 0, 1, 2... So it prints to file1 , file2 ... {print >> "file"a}将行打印到名为“ file” +变量a文件,该文件恰好是0、1、2 ...,因此将其打印到file1file2 ...

Test 测试

$ cat a
Jul 11 xxxx  xxxx start xxxxx
....
....
....
Jul 11 xxxx  xxxx stop xxxxx
Jul 11 xxxx  xxxx start xxxxx
here begins 2nd file
....
....
....
Jul 11 xxxx  xxxx stop xxxxx
$ awk '{if ($0 ~ /start/) {a++}} {print >> "file"a}' a
$ cat file1
Jul 11 xxxx  xxxx start xxxxx
....
....
....
Jul 11 xxxx  xxxx stop xxxxx
$ cat file2
Jul 11 xxxx  xxxx start xxxxx
here begins 2nd file
....
....
....
Jul 11 xxxx  xxxx stop xxxxx

对于awk,“ if($ 0〜)”是隐式的。

awk 'BEGIN{a=0;b=0}  /start/{a++;b=1}    (b==1){print >> "file"a}   /stop/{b=0}' input_file.txt

暂无
暂无

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

相关问题 如何使用Linux将文件拆分为不同行的多个文件? - How to use linux to split a file into several files with different lines? 将LINUX中的一个巨大文件拆分为多个小文件(每个小于100MB),在文件匹配的特定行分割 - Split a huge file in LINUX into multiple small files (each less than 100MB) splitting at a specific line with pattern match 如何根据给定的模式将linux中的文本文件从文件底部拆分到顶部 - How to split a text file in linux from the bottom of the file to the top based on a given pattern Linux SED 脚本找到匹配模式的第一行并将其删除 - Linux SED script find the 1st line that match pattern and delete it 如何使用 linux shell 脚本将行分隔的 JSON 拆分为多个文件 - How to split line delimited JSON into many files using linux shell script 脚本比较不同文件中的文本 - script to compare text in different files 如何使用 linux 中的连接命令将两个不同长度和不同列的文本文件与 header 匹配 - How to match two different length and different column text file with header using join command in linux 如何在linux中为具有特定(范围)模式的所有文件执行作业提交脚本 - How to execute a job-submitting script for all the files with a specific (range of) pattern in linux 如何根据模式将文件分为两个文件? - How to split a file into two files based on a pattern? 如何删除 linux 中具有特定模式的文件? - how to delete files have specific pattern in linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM