Solved

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

Posted on 2011-02-16
3
311 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
ID: 34912083
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
ID: 34912113
worked perfect, thanks sjklein42.....
0
 

Author Closing Comment

by:areyouready344
ID: 34912116
excellent solution...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
git fat pull happens nothing in centos 6.3 python 2.6.6 33 219
stftime format 4 56
pipe to sed or perl, please help 5 130
perl to convert excel to csv 3 254
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

838 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