Deleting newlines in a text file

T

toomanyjoes

Hello,

I'm brand new to perl. I need to delete newlines from a text file but
only in the case that the line does not begin with a number. Then write
the results into a new text file.

For instance my text file looks like this.

1sampletextsampletextsamplet22tsampletextsampletextsampletext
2sampletextsampletextsampletextsam56etextsampletextsampletext
3sampletextsampletextsampletextsampletextsampletextsampletext
8sampletextsampletextsampletex765mpletextsampletextsampletext
9sampletextsampletextsampletextsampletextsampletextsampletext
15sampletextsampletextsampletextsampletextsampletextsampletext
sampletextsampletextsampletextsample324tsampletextsampletext
18sampletextsampletextsampletextsampletextsampletextsampletext
sampletextsampletextsampletextsampletextsampletextsampletext
sampletextsampletext566pletextsampletextsampletextsampletext
22sampletextsampletextsampletextsampletextsampletextsampletext
1sampletextsampletextsamplete467ampletextsampletextsampletext

As you can see I would like every line to START with a number.


Also after this is done I need another script that will grab the number
at the beginning and put it in a database field and then grab all the
"sampletext" up until the next number and put that in another database
field. This taking into account that there could be numbers within the
sampletext that are not found at the beginning of a line. These numbers
should be taken as part of the sample text and not confused with the
numbers at the beginning of the line.(A little harder I'm sure) Any
questions you guys can help me with I'll appreciate.


Thanks!

Joe
 
J

Jürgen Exner

I'm brand new to perl. I need to delete newlines from a text file but
only in the case that the line does not begin with a number.
[...]

Just chomp() the line iff it starts with a digit

chomp if (/^\d/);

jue
 
C

Chris Mattern

Jürgen Exner said:
I'm brand new to perl. I need to delete newlines from a text file but
only in the case that the line does not begin with a number.
[...]

Just chomp() the line iff it starts with a digit

chomp if (/^\d/);
No, that doesn't do what he wants. What he wants to do is
chomp iff the *next* line does *not* begin with a digit.

my $last_line = <>;
while (<>) {
chomp ($last_line) if (/^\D/);
print $last_line;
$last_line = $_;
}
print $last_line;

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
J

Jay Tilton

(e-mail address removed) wrote:

: I'm brand new to perl. I need to delete newlines from a text file but
: only in the case that the line does not begin with a number.

[snip]

: As you can see I would like every line to START with a number.

"...only in the case that the _next_ line does not begin with a number"
seems a more accurate description.

perl -pe "chomp;print $/ if $.>1 and /^\d/" file1 > file2

(Alter quotes to suit your shell, naturally.)
 
T

Tad McClellan

Any
questions you guys can help me with I'll appreciate.


I notice that there *were no* questions in your article.
(questions end with a question mark.)

Did you mean to ask some questions?

Show us what you have tried so far, and we will help you fix it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top