• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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
0
samabdelhamid
Asked:
samabdelhamid
  • 3
  • 3
1 Solution
 
ozoCommented:
perl -0e '$/=<>;  $_=<>; chomp; print' 4Lines.txt  AllContents.txt > NewContents.txt
0
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now