Shell scrip to delete all text after mathing n lines

Hi all,
I have been using sed commands to try and accomplish the following part of a script that is 95% complete. I am unable to get the logic for the following problem.



I have the following file

File : AllContents.txt
Calibre/Item:  Stock
Make:  Remington
Model:  700 synthetic
Condition:  Excellent
Price: $200
Calibre/Item:  Knife Set
Make:  Browning
Model:  Fixed & Folding Knife Set
Condition:  New
Price: $85
Calibre/Item:  22LR
Make:  Smith & Wesson
Model:  41
Action:  Semi auto
Scope/Sights:  Adjustable
Condition:  Excellent
Price: $1000
Calibre/Item:  204
Make:  Stiller

Open in new window


I want to delete everything in this file (and save it to a new file) based on teh contents of this file which contains 4 lines:
(This file only always contains 4 lines)

4Lines.txt
Calibre/Item:  Knife Set
Make:  Browning
Model:  Fixed & Folding Knife Set
Condition:  New

Open in new window


so the resulting file that should be produced is this

NewContents.txt
Calibre/Item:  Stock
Make:  Remington
Model:  700 synthetic
Condition:  Excellent
Price: $200

Open in new window


I am unsure the best way to go about this, the special characters in the file 4lines.txt is trowing me off a little.

Cheers
samabdelhamidAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
perl -0e '$/=<>;  $_=<>; chomp; print' 4Lines.txt  AllContents.txt > NewContents.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samabdelhamidAuthor Commented:
Thanks OZO. A great solution, however I probably did not make my question 100% clear.

I wanted to remove everything AFTER (and including) the text in 4Lines.txt

It is now currently just removing the text found in 4Lines.txt and leaving all the remaining text in there.

I dont know enough about perl to make the appropriate changes.
Cheers
0
ozoCommented:
That is not what it is doing for me.  Are you sure you copied the command accurately?
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

samabdelhamidAuthor Commented:
Thanks OZO.
The problem I had was that I had a New Line character after the last line on 4Lines.txt
It works when this is removed...
0
samabdelhamidAuthor Commented:
ok. Having more problems now, when something has changed. The above perl script does not work with the following, when All contents and 4Lines changes.
why is the perl script so sensitive?

Allcontents.txt

Calibre/Item:  22-250
Make:  Tikka
Model:  Varmint Stainless
Action:  Bolt repeater
Scope/Sights:  Nikko Stirling
Condition:  Excellent
Price: $1200
Calibre/Item:  32-20 WCF
Make:  Winchester
Model:  1892
Action:  Lever repeater
Scope/Sights:  Open sights
Condition:  Good
Price: $550
Calibre/Item:  32-40 WCF
Make:  Winchester
Model:  1894
Action:  Lever repeater
Scope/Sights:  Open sights
Condition:  Fair
Price: $775
Calibre/Item:  Magazine
Make:  Anshutz
Model:  1430 for 22 Hornet
Condition:  New
Price: $150
Calibre/Item:  257 Roberts
Make:  Ruger
Model:  M77 Mk II
Action:  Bolt repeater
Scope/Sights:  3-9x40 Simmons
Condition:  Excellent
Price: $1000
Calibre/Item:  Stock
Make:  Remington
Model:  700 synthetic
Condition:  Excellent
Price: $200
Calibre/Item:  Knife Set
Make:  Browning
Model:  Fixed & Folding Knife Set
Condition:  New
Price: $85
Calibre/Item:  22LR
Make:  Smith & Wesson
Model:  41
Action:  Semi auto
Scope/Sights:  Adjustable
Condition:  Excellent
Price: $1000
Calibre/Item:  204
Make:  Stiller
Model:  Tac 30
Action:  Bolt repeater
Scope/Sights:  None
Condition:  Excellent
Price: $2950
Calibre/Item:  Chokes
Make:  Muller
Model:  Blaser 12G
Condition:  Excellent
Price: $275
Calibre/Item:  30-06
Make:  FN Famage
Model:  98 Colombian Mauser
Action:  Bolt repeater
Scope/Sights:  Open sights
Condition:  Good
Scope/Sights:  Open sights
Condition:  Good
Price: $450
Calibre/Item:  7mm-08
Make:  Marlin
Model:  XS7S
Action:  Bolt repeater
Scope/Sights:  Weaver bases
Condition:  Very Good
Price: $450
Calibre/Item:  6.5x55
Make:  Zastava
Model:  LKM70
Action:  Bolt repeater
Scope/Sights:  Open sights
Condition:  Excellent
Price: $550
Calibre/Item:  .22LR
Make:  Sportco
Model:  90
Action:  Pump repeater
Scope/Sights:  Open sights
Condition:  Good
Price: $650
Calibre/Item:  Cases
Make:  Weatherby
Model:  300 Wby Mag x 126
Condition:  Excellent
Price: $200

Open in new window


4Lines.txt

Calibre/Item:  Stock
Make:  Remington
Model:  700 synthetic
Condition:  Excellent

Open in new window


Expected Output

]Calibre/Item:  22-250
Make:  Tikka
Model:  Varmint Stainless
Action:  Bolt repeater
Scope/Sights:  Nikko Stirling
Condition:  Excellent
Price: $1200
Calibre/Item:  32-20 WCF
Make:  Winchester
Model:  1892
Action:  Lever repeater
Scope/Sights:  Open sights
Condition:  Good
Price: $550
Calibre/Item:  32-40 WCF
Make:  Winchester
Model:  1894
Action:  Lever repeater
Scope/Sights:  Open sights
Condition:  Fair
Price: $775
Calibre/Item:  Magazine
Make:  Anshutz
Model:  1430 for 22 Hornet
Condition:  New
Price: $150
Calibre/Item:  257 Roberts
Make:  Ruger
Model:  M77 Mk II
Action:  Bolt repeater
Scope/Sights:  3-9x40 Simmons
Condition:  Excellent
Price: $1000

Open in new window


Current output is the entire contents of AllContents.txt
0
ozoCommented:
When I cut and paste 4Lines.txt from http:#a39475483
I get a 5th line with lots of spaces, which is not found in AllContents.txt
Assuming that is your problem, this will only use the first 4 lines of 4Lines.txt and ignore any extra lines

head -4 4Lines.txt | perl -0e '$/=<>; $_=<>; chomp; print' -  AllContents.txt  >  NewContents.txt
or
perl -0e '($/=<>)=~s/((.*?\n){4}).*/$1/s; $_=<>; chomp; print' 4Lines.txt  AllContents.txt >  NewContents.txt
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.