Solved

Delete lines shorter than 8 characters

Posted on 2008-06-24
13
392 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Sessions in WordPress 8 31
Why do people dis php? 5 49
Blocking submission of a web-based form 6 20
error log using ftp 7 40
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…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

770 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