Solved

Delete lines shorter than 8 characters

Posted on 2008-06-24
13
399 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

792 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