Operating System - HP-UX
1827687 Members
3575 Online
109967 Solutions
New Discussion

Deleting patterns in a file

 
Virumandi
Frequent Advisor

Deleting patterns in a file

Hi to all.,

My text file looks like this...

This is the Starting of the File

Arg1
Arg2
Comments XYZ

This is the contiinuation of the File
Do an if statement for Conditions

Arg3
Arg4
Comments ABC

exit

I want to delete the table between the pattern &

So my output after deleted the table will looks like...


This is the Starting of the File


This is the contiinuation of the File
Do an if statement for Conditions


exit

I am breaking my head from last 2 days for this issue.. Any body expert in scripting help me give a conclusion.


Thanks & Regards
Suseendran .A
3 REPLIES 3
Hein van den Heuvel
Honored Contributor

Re: Deleting patterns in a file



perl -ne '$skip = 0 if /^/; print unless $skip; $skip = 1 if /^/' tmp.txt

Enjoy,
Hein.

Peter Nikitka
Honored Contributor

Re: Deleting patterns in a file

Hi,

an awk solution:

awk '!skip {print}
$1 == "" {skip=1}
$1 == "" {if(skip) print;skip=0}' your/file

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: Deleting patterns in a file


[ For a short while there was a duplicate entry for this topic to which I also replied.
That reply was still in my paste buffer, so here is it is again. Hein. ]

----

For fun, a minor variation on my earlier answer:

perl -ne "$x=0 if /^
The trick here agains is to test for before printing such that the line itself is candidate. Test for after the potential print to make sure it is already printed, but switch of printing when seen.

The "$x or print" is a litte cute.
Perl tests $x, if it is one, then the total result is true and thus it does nto need to print. If it is false (initial state) the print must be done to find the result of the total expression.

The /^/ anchors the test for end to the begin of the line for the fastest possible match/mismatch and to make sure we do not trigger on a spurious in the text.

Regards,
Hein.