anji
anji

Reputation: 103

How to print the output by using awk or sed

I have the below text in a file. From this if search for "Fileset", need to get the output until second "N". I have tried using awk and sed but not able to get the output as expected.

Job: name=jobname JobType=66 level= Priority=10 Enabled=1
     MaxJobs=1 Resched=0 Times=0 Interval=1,800 Spool=0 WritePartAfterJob=1
     Accurate=0
  --> Client: Name=clientname Enabled=1 Address=XXXXXX FDport=9102 MaxJobs=1
      JobRetention=14 days  FileRetention=14 days  AutoPrune=1
  --> Catalog: name=MyCatalog address=*None* DBport=0 db_name=bacula
      db_driver=*None* db_user=bacula MutliDBConn=0
  -->Fileset: name=test_host
     O MZof
     N
     I /u01
     N

From the above, need to get only below output.

Fileset: name=test_host
         O MZof
         N
         I /u01
         N

Tried below command

awk '/Fileset:/ {flag=1;next} /N/{flag=0} flag {print}' file

Where Fileset is pattern /N/ is for end flag

Upvotes: 0

Views: 65

Answers (1)

Quasímodo
Quasímodo

Reputation: 4004

If you want the second N to stop the processing, start flag with 2 when Fileset: is found, print lines while flag!=0 and then decrement flag for each line with N.

awk '/Fileset:/{flag=2};flag;/N/&&flag{flag--}' file

Upvotes: 2

Related Questions