如何从txt文件中读取每一行并从Shell脚本中以“ Created_Date”开头的行开始打印

时间:2018-08-07 14:43:15

标签: bash shell

5G_Fixed_Wireless_Dashboard_TestScedule||||||||||||||||^M

Report Run Date||08/07/2018|||||||||||||||||||||^M

Requesting User Company||NEW|||||||||||||||||||||^M

Report Criteria|||||||||||||||||||||||^M

" Service Job Updated from Date:

  Service Job Updated to Date:

  Service Job Created from Date: 08/06/2018

   Service Job Created to Date:

 Service Job Status:

Resolution Code:"|||||||||||||||||||||||^M

Created Date|Job Status|Schedule Date|Job 
    Number|Service Job Type|Verizon Customer Order 
    Number|Verizon Location Code|Service|Installation 
    Duration|Part Number

我要从创建日期开始打印。结果    文件应如下所示。

   Created Date|Job Status|Schedule Date|Job 
   Number|Service Job Type|Verizon Customer Order 
   Number|Verizon Location Code|Service|Installation 
   Duration|Part Number

在您将我与其他问题联系在一起之后,我尝试了以下内容。但是我的要求是将结果打印到同一文件中。

FILELIST = find $MFROUTDIR -maxdepth 1 -name "XXXXXX_5G_Order_*.txt"

对于$ FILELIST中的nextFile;执行

    cat $nextFile | sed -n -e '/Created Date/,$p'

完成

通过编写以上代码行,输出将打印在控制台上。您能否建议使用某种方式将其打印到同一文件中。

1 个答案:

答案 0 :(得分:1)

这可以通过简单的awk命令轻松完成:

awk '/^Created Date/{p=1} p' file

Created Date|Job Status|Schedule Date|Job
    Number|Service Job Type|Verizon Customer Order
    Number|Verizon Location Code|Service|Installation
    Duration|Part Number

当遇到以p开头的行时,我们将标志1设置为Created Date。稍后,我们使用awk默认操作在p==1时打印每一行。


参考: