Solved

Delete lines shorter than 8 characters

Posted on 2008-06-24
13
375 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
The viewer will learn how to count occurrences of each item in an array.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now