473,416 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

change $/

I have a datafile with the following format from which I need to extract parts of the text and copy to a seperate file :

[HTML]
Header (line 1)
---------------------------
(line 2starts)
Content - Content
Content - Content
Content - Content

Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -
(line break)
Subheader
Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Co
(linebreak)
Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content -
( line2 ends)
----------------
Header (line 3)
-----------------
(line 4 starts)
Content - Content
Content - Content
Content - Content

Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -
(line break)
Subheader
Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Co
(linebreak)
Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content
(line 4 ends)
[/HTML]


The file contains text that repeats the above pattern many times (is often very large).

I would like to change the record seperator so that it reads in the header as on line and the rest of the record as another line. --- isnt part of the file - just a visual cue I put in to demonstrate different lines. Is this possible?
Jun 21 '07 #1
1 1542
miller
1,089 Expert 1GB
Dealing with the record separater is a rather advanced method for parsing a file. I suggest that you first attempt to parse the file using regular techniques. This will often demonstrate if it is even possible to do what you're wanting.

Also please note that your terminology is confusing. Instead of "line 1", I believe you mean record 1. As each of the main content sections contain "lines".

Expand|Select|Wrap|Line Numbers
  1. for (my $line = <DATA>; ! eof(DATA);) {
  2.     my $header = $line;
  3.  
  4.     my $content = '';
  5.     while (defined($line = <DATA>)) {
  6.         last if $line =~ /^Header/;
  7.         $content .= $line;
  8.     }
  9.  
  10.     print $header;
  11.     print "Content = " . length($content) . " characters\n";
  12. }
  13.  
  14. __DATA__
  15. Header (section 1)
  16. Content - Content
  17. Content - Content
  18. Content - Content
  19. Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -
  20. Subheader
  21. Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Co
  22. Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content -
  23. Header (section 2)
  24. Content - Content
  25. Content - Content
  26. Content - Content
  27. Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -
  28. Subheader
  29. Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Co
  30. Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content -Content - Content
  31.  
Output
Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl
  2. Header (section 1)
  3. Content = 515 characters
  4. Header (section 2)
  5. Content = 523 characters
  6.  
- Miller
Attached Files
File Type: txt scratch.txt (1.3 KB, 464 views)
Jun 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: J.C. Flores | last post by:
Hello all, First of all, I must state that I'm new to SQL Server, but have been a long-time software guy for quite some time. Please excuse the potential simplicity of the solution to my...
1
by: Spike | last post by:
Hello! Im going to make a javascript for changing alot of images. But im not sure how to do it., where to start.. Ok, first.. this is the isue. I have 3 images(I call them 1a-3a). when u...
0
by: beanweed | last post by:
BACKGROUND ---------- I have an ASP.NET application having two panels. In one panel, an XML document, transformed using xsl, is displayed. In the other panel are some controls that allow a user...
5
by: Hank | last post by:
My Access 2000 code has been running for several years in our main plant. Now we need to install it, as a stand-along application, at remote sites, some of which are out of state. My problem is...
10
by: Fabrizio | last post by:
(Sorry for the crosspost, but I really don't know which is the right newsgroup!) Hi all, I try to change the password to a user that as to change the password at first logon: try {
4
by: active | last post by:
It appears to me that if I change the Control's client size the controls size does not change to agree with the new size. Does it work like the VB6 ScaleWidth and ScaleHeight? That is after I...
3
by: Tom | last post by:
I am writing a Visual basic .Net database application. There are many forms that first let you select and look at a DB record and then when you click a "modify" button you are allowed to change...
2
by: J | last post by:
Hello. I apologize if this isn't the appropriate group for this question but I was wondering if it's possible to allow regular windows domain users to change their passwords through an .asp page? ...
11
by: Simon | last post by:
Dear reader, The syntax for the VBA code to change the RowSource of a Master Report is: Me.RowSource = "TableOrQueryName"
1
by: monkey1001 | last post by:
my program is suppose to show my due change and i got it working but my change and coins are wrong how can i improve it thank you..(its supposed to be in java)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.