Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Delete lines shorter than 8 characters

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
lucavilla
Asked:
lucavilla
  • 6
  • 3
  • 3
  • +1
1 Solution
 
hernst42Commented:
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
 
Adam314Commented:
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
 
ozoCommented:
perl -i.bak -ne "print if length > 8" file
0
Technology Partners: 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!

 
lucavillaAuthor Commented:
hernst42, Adam314 and ozo, I tried all your solutions but all fails with (different) error
0
 
Adam314Commented:
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
 
lucavillaAuthor Commented:
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
 
lucavillaAuthor Commented:
for the "On windows:"  solution
0
 
ozoCommented:
are you sure you copied the command correctly?
0
 
lucavillaAuthor Commented:
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
 
ozoCommented:
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
 
lucavillaAuthor Commented:
Thanks Ozo for the interesting clarification
0
 
Adam314Commented:
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
 
lucavillaAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now