Delete lines in a file

A

Adam

Hello:

I am trying to solve the following problem:

A file, on each line contains typically 5 columns (separated by tabs)
of numbers. Ocassionally, on a line, there appears a single number in
the first column. I want to delete occurrences of such lines (i.e.
lines that only contain a single number.

For example

1.2345 2.3456 3.4567 4.5678 5.6789 ---> Typical line
23 ---> Atypical line (delete this
occurence)

How can I do this in Perl ?

Thanks
 
J

Jürgen Exner

Adam said:
A file, on each line contains typically 5 columns (separated by tabs)
of numbers. Ocassionally, on a line, there appears a single number in
the first column. I want to delete occurrences of such lines (i.e.
lines that only contain a single number.

Your Question is Asked Frequently, see
perldoc -q "delete a line"

jue
 
P

Paul Lalli

Jürgen Exner said:
Your Question is Asked Frequently, see
perldoc -q "delete a line"

I disagree that Tie::File is needed for this operation. This would
suffice, no?

perl -ni.bak -e'print unless /^\d+$/' file.txt

(obviously, may need to modify the regexp if decimals or other
representations of numbers are allowed on the atypical lines)

Paul Lalli
 
J

J. Romano

A file, on each line contains typically 5 columns (separated by tabs)
of numbers. Ocassionally, on a line, there appears a single number in
the first column. I want to delete occurrences of such lines (i.e.
lines that only contain a single number.

For example

1.2345 2.3456 3.4567 4.5678 5.6789 ---> Typical line
23 ---> Atypical line (delete this
occurence)


Try this:

In UNIX:
perl -ne 'print unless m/^\d+$/' input.txt > output.txt

In DOS (Win32):
perl -ne "print unless m/^\d+$/" input.txt > output.txt

The file "input.txt" will be the file you already have, and
"output.txt" will be the file that will be created, which will be
exactly like "input.txt" except that it won't have any lines
consisting of a single number (i.e., the atypical lines).

I hope this helps.

-- Jean-Luc
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top