-1
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

I want to print starting from Created Date. The result file should be something like below.

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

I have tried the following lines after you people linked me to some other questions. But my requirement is to print the result to the same file.

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

for nextFile in $FILELIST;do

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

done

By writing above lines of code, output is printed on console. Could you please suggest some way to print it in same file.

  • 2
    Have you tried anything? This can be done in multiple ways e.g. bash, sed, awk etc – anubhava Aug 07 '18 at 14:45
  • @NavyaNaiduChamidisetty: Not in comments, add this code by editing your question. – anubhava Aug 07 '18 at 15:06
  • First, you really don't want to write back to the same file. However, since you will choose to ignore that advice, you have many options. Possibly you will be content to simply add `-i` to your sed invocation. – William Pursell Aug 07 '18 at 15:24
  • @WilliamPursell When I add -i to sed command, it is saying "sed: no input files". I used cat $nextFile | sed -i -n -e '/Created Date/,$p' – Navya Naidu Chamidisetty Aug 07 '18 at 15:36
  • @WilliamPursell It worked when I added cat $nextFile | sed -i -n -e '/Created Date/,$p' $nextFile. Thanks for your response – Navya Naidu Chamidisetty Aug 07 '18 at 15:44
  • @WilliamPursell I have a small issue here like when a newfile is created with data it is appending ^M character after each row. Could you please help me resolving this. Actually I added this line "sed -i -e 's/\^M//g' $nextFile" after cat statement as mentioned in above comment. But it did not work. I am still seeing ^M – Navya Naidu Chamidisetty Aug 09 '18 at 17:40
  • The ^M is most easily resolved with `dos2unix` – William Pursell Aug 09 '18 at 21:52

1 Answers1

1

This can be easily done with a simple awk command:

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

We set a flag p to 1 when we encounter a line that starts with Created Date. Later we use awk default action to print each line when p==1.


References:

Community
  • 1
  • 1
anubhava
  • 664,788
  • 59
  • 469
  • 547