Solved

Add a marker to the first row of a record using Perl

Posted on 2011-02-16
3
284 Views
Last Modified: 2012-05-11
I have this data record using a input record separator as __Data__ and would like to add a marker to the first row of this data record.

Current,

Here's the data record....

__Data__
scsi_test_hotswap
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld

__Data__
scsi_test_memory_leakage
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
3. dldldld - dldldldldldldldld
4. dldldld - dldldldldlldldldld

Ideal output would be

Here's the data record....

__Data__
@scsi_test_hotswap
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld

__Data__
@scsi_test_memory_leakage
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
3. dldldld - dldldldldldldldld
4. dldldld - dldldldldlldldldld

I know the solution would be to use a regex search and replace but not sure how to construct it....
0
Comment
Question by:areyouready344
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
save as fixit.pl

perl fixit.pl infile.txt >outfile.txt

$fixNext = 0;
while ( <> )
{
	s/[\r\n]//g;
	if ( $fixNext ) { $_ = '@' . $_; $fixNext = 0; }
	if ( /__Data__/ ) { $fixNext = 1; }
	print $_ . "\n";
}

Open in new window

0
 

Author Comment

by:areyouready344
Comment Utility
worked perfect, thanks sjklein42.....
0
 

Author Closing Comment

by:areyouready344
Comment Utility
excellent solution...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now