LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2012, 04:32 AM   #1
menylea
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
how to find continuation of line with PERL


Hello experts,

here is a short question for you.

Im trying to use PERL in order to pharse a text document( netlist of a circuit), that contain lines like that:

net147(L1 0)JDJDJDS DSHF FSFH
net1124( C1 0) DGDG SHSH SJ01 GF ....\
GFGFGDSGDG HHSHSH
.
.
.


As you can see the line that end with "\" mean that the next line is a continuation of this line.
the problem is that some lines finish with "\", while other lines are short and dont have this "\".
sometimes a line can be divided this way into 3 or 4 lines( with "\" at the end of each line).

so, I guess if I want to run over a text file and make some modification to the lines , I first need to read the input file, and get the lines correctly.
field separtor?, selector? if loop?
I remember that there is a command to check this right?

Thank you

Meny
 
Old 03-08-2012, 05:38 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Code:
@arr1 = ('net147(L1 0)JDJDJDS DSHF FSFH', 'net1124( C1 0) DGDG SHSH SJ01 GF ....\\', ' GFGFGDSGDG HHSHSH', 'AA','BB\\', 'CC\\','DD', 'EE');
for $var1 ( @arr1)
{
    if( $var1 !~ /\\$/ )
    {
        print "$var1\n";
    }
    else
    {
        $var2 = length($var1);
        $var3 = substr($var1,0,$var2 -1);
        print "$var3";
    }
}
Code:
net147(L1 0)JDJDJDS DSHF FSFH
net1124( C1 0) DGDG SHSH SJ01 GF .... GFGFGDSGDG HHSHSH
AA
BBCCDD
EE
That should give you the idea. Basically you check using regex for line ending with '\' and remove that char before printing (or whatever).
NB: I've had to cheat and use 2 \\ in the array elements, because I'm reading strings, not file rec, but the rest of the code should be the same.
The regex still needs 2 \\ (ie one extra) to escape the \ because it otherwise escapes the /.

HTH
See http://perldoc.perl.org/functions/substr.html
http://perldoc.perl.org/perlrequick.html
http://perldoc.perl.org/perlretut.html

Actually, you could just use substr() to check the last char instead of a regex; your choice

Also, be careful to check/enforce no <space> char between '\' and the end-of-line char ...
 
Old 03-14-2012, 02:51 AM   #3
menylea
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for the help. I undersant batter now.
However, I dont Understand the firs line of your code = @arry=....
you took the text file and add ' between lines...
I cant do it because this is 2000 line file. too big to go over by end and add '.
How I can load the text file correctly into an array with out doing manual change?

thx

meny
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl Find and replace line in crontab Sir Todd Programming 2 02-14-2012 03:55 PM
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 06:09 PM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 07:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07:46 PM
PERL:how to find a blank line in a file--regular expression littletransformer Programming 4 03-27-2008 07:55 PM


All times are GMT -5. The time now is 04:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration