Solved

Delete lines shorter than 8 characters

Posted on 2008-06-24
13
405 Views
Last Modified: 2010-04-21
I have a file containing this:

Domenico
Claudio
Alex
Christian
Chiara
Francesca
Federica

How can I automatically delete with PHP or Perl all the lines shorter that 8 characters obtaining this?

Domenico
Christian
Francesca
Federica
0
Comment
Question by:lucavilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21860657
Would go like this in PHP:
<?php
 
$fp = fopen('input.txt');
while(!feof($fp)) {
   $line = fgets($fp, 10000);
   if (strlen(trim($line) <= 8)) {
      continue;
   }
   echo $line;
}

Open in new window

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 21860681
Replace test1.txt with the name of your input file.  
On windows:
    perl -ne "chomp;print \"$_\n\" if length()>=8" input.txt > output.txt

On anythign else:
    perl -ne 'chomp;print "$_\n" if length()>=8' input.txt > output.txt

Or to edit the same file:
    perl -i.bak -ne "chomp;print \"$_\n\" if length()>=8" input.txt
    perl -i.bak -ne 'chomp;print "$_\n" if length()>=8' input.txt
0
 
LVL 84

Expert Comment

by:ozo
ID: 21860684
perl -i.bak -ne "print if length > 8" file
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lucavilla
ID: 21860786
hernst42, Adam314 and ozo, I tried all your solutions but all fails with (different) error
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21860848
You are going to have to be more specific.  What error do you get with what code?
Do you have perl installed?
If not, you will need to install it to use what either I or ozo posted.
0
 

Author Comment

by:lucavilla
ID: 21861595
Second try:


hernst42:

With your solutions I get an infinite number of error lines:

C:\PHP\php yourscript.php
PHP Warning:  feof(): supplied argument is not a valud stream resource in C:\PHP\yourscript.php on line 4
PHP Warning:  fgets(): supplied argument is not a valud stream resource in C:\PHP\yourscript.php on line 5
PHP Warning:  feof(): supplied argument is not a valud stream resource in C:\PHP\yourscript.php on line 4
PHP Warning:  fgets(): supplied argument is not a valud stream resource in C:\PHP\yourscript.php on line 5
PHP Warning:  feof(): supplied argument is not a valud stream resource in C:\PHP\yourscript.php on line 4
PHP Warning:  fgets(): supplied argument is not a valud stream resource in C:\PHP\yourscript.php on line 5
...
(infinite? I ended the script with CTRL+C)



Ozo:

your solution generates an empty output.. (input.txt 0 bytes and input.txt.bak 65 bytes that is the original)



Adam314:

perl -ne "chomp;print \"$_\n\" if length()>=8" input.txt > output.txt

Works!!!

Problem solved. Thanks for the minimalist solution!
0
 

Author Closing Comment

by:lucavilla
ID: 31470386
for the "On windows:"  solution
0
 
LVL 84

Expert Comment

by:ozo
ID: 21861788
are you sure you copied the command correctly?
0
 

Author Comment

by:lucavilla
ID: 21864629
ozo, I also tested variants like "length>8" without spaces but nothing...
Perhaps the correct syntax is that of Adam314:  "if length()>8"  or better  "if length()>=8" in my case
0
 
LVL 84

Expert Comment

by:ozo
ID: 21867969
I used > instead of >= because I was counting the newline character instead of removing it and then putting it back.
length > 8 or length() > 8 or (length) > 8 should make no difference
if it was < 8, that would be ambiguous without parenthesis because it could look like the start of <>


0
 

Author Comment

by:lucavilla
ID: 21868309
Thanks Ozo for the interesting clarification
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21868664
If on windows, the line ending could be two characters, \r\n.  Would that still work?  That's why I used chomp, in case of something like that.
0
 

Author Comment

by:lucavilla
ID: 21885366
Ozo, I tried again your solution and this time it worked!
I'm sure that it was my fault. Sorry for it!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 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