Remove lines from a file: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Add Seed7 example)
Line 315: Line 315:
3.txt: done
3.txt: done
4.txt: done
4.txt: done

=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
include "osfiles.s7i";

const proc: removeLines (in string: fileName, in integer: start, in integer: count) is func
local
var file: inFile is STD_NULL;
var file: outFile is STD_NULL;
var integer: lineNumber is 1;
var string: line is "";
begin
inFile := open(fileName, "r");
outFile := open(fileName & ".tmp", "w");
while hasNext(inFile) do
line := getln(inFile);
if lineNumber < start or lineNumber >= start + count then
writeln(outFile, line);
end if;
incr(lineNumber);
end while;
close(inFile);
close(outFile);
removeFile(fileName);
moveFile(fileName & ".tmp", fileName);
end func;

const proc: main is func
begin
if length(argv(PROGRAM)) = 3 then
removeLines(argv(PROGRAM)[1], integer parse (argv(PROGRAM)[2]), integer parse (argv(PROGRAM)[3]));
end if;
end func;</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 10:57, 6 September 2011

Remove lines from a file is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to demonstrate how to remove a specific line or a number of lines from a file. This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed). For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2

Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed. An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.

C++

<lang cpp>#include <fstream>

  1. include <iostream>
  2. include <string>
  3. include <cstdlib>
  4. include <list>

void deleteLines( const std::string & , int , int ) ;

int main( int argc, char * argv[ ] ) {

  if ( argc != 4 ) {
     std::cerr << "Error! Invoke with <deletelines filename startline skipnumber>!\n" ;
     return 1 ;
  }
  std::string filename( argv[ 1 ] ) ;
  int startfrom = atoi( argv[ 2 ] ) ;
  int howmany = atoi( argv[ 3 ] ) ;
  deleteLines ( filename , startfrom , howmany ) ;
  return 0 ;

}

void deleteLines( const std::string & filename , int start , int skip ) {

  std::ifstream infile( filename.c_str( ) , std::ios::in ) ;
  if ( infile.is_open( ) ) {
     std::string line ;
     std::list<std::string> filelines ;
     while ( infile ) {

getline( infile , line ) ; filelines.push_back( line ) ;

     }
     infile.close( ) ;
     if ( start > filelines.size( ) ) {

std::cerr << "Error! Starting to delete lines past the end of the file!\n" ; return ;

     }
     if ( ( start + skip ) > filelines.size( ) ) {

std::cerr << "Error! Trying to delete lines past the end of the file!\n" ; return ;

     }
     std::list<std::string>::iterator deletebegin = filelines.begin( ) , deleteend ;
     for ( int i = 1 ; i < start ; i++ )

deletebegin++ ;

     deleteend = deletebegin ;
     for( int i = 0 ; i < skip ; i++ )

deleteend++ ;

     filelines.erase( deletebegin , deleteend ) ;
     std::ofstream outfile( filename.c_str( ) , std::ios::out | std::ios::trunc ) ;
     if ( outfile.is_open( ) ) {

for ( std::list<std::string>::const_iterator sli = filelines.begin( ) ; sli != filelines.end( ) ; sli++ ) outfile << *sli << "\n" ;

     }
     outfile.close( ) ;
  }
  else {
     std::cerr << "Error! Could not find file " << filename << " !\n" ;
     return ;
  }

}</lang>

Icon and Unicon

<lang Icon>procedure main() # remove lines

  removelines("foo.bar",3,2) | stop("Failed to remove lines.")

end

procedure removelines(fn,start,skip)

  f := open(fn,"r") | fail                  # open to read
  every put(F := [],|read(f))               # file to list
  close(f)
  if *F < start-1+skip then fail
  every F[start - 1 + (1 to skip)] := &null # mark delete
  
  f := open(fn,"w") | fail                  # open to rewrite
  every write(\!F)                          # write non-nulls
  close(f)
  return                                    # done

end</lang>

J

<lang j>removeLines=:4 :0

 'count start'=. x
 file=. boxxopen y
 lines=. <;.2]1!:1 file
 (;lines {~ <<< (start-1)+i.count) 1!:2 file

)</lang>

Thus: <lang bash>$ cal >cal.txt $ cat cal.txt

     July 2011

Su Mo Tu We Th Fr Sa

               1  2
3  4  5  6  7  8  9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31</lang>

<lang j> 2 1 removeLines 'cal.txt'</lang>

<lang bash>$ cat cal.txt

               1  2
3  4  5  6  7  8  9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31</lang>

Note that this code assumes that the last character in the file is the line end character.

Liberty BASIC

It's always a bit dangerous to experiment with code that alters files. Un-rem the 'kill' to remove the temp file and the next line so the file is renamed to the original! <lang lb> call removeLines "foobar.txt", 1, 2 end

sub removeLines filename$, start, count

   open filename$ for input as #in
   open filename$ + ".tmp" for output as #out
   lineCounter = 1
   firstAfterIgnored = start + count
   while not(eof(#in))
       line input #in, s$
       if lineCounter < start or lineCounter >= firstAfterIgnored then
           print #out, s$
       end if
       lineCounter = lineCounter + 1
   wend
   close #in
   close #out
   'kill filename$
   'name filename$ + ".tmp" as filename$

end sub </lang>


Lua

<lang lua>function remove( filename, starting_line, num_lines )

   local fp = io.open( filename, "r" )
   if fp == nil then return nil end
   content = {}
   i = 1;
   for line in fp:lines() do
       if i < starting_line or i >= starting_line + num_lines then

content[#content+1] = line end i = i + 1

   end
   if i > starting_line and i < starting_line + num_lines then

print( "Warning: Tried to remove lines after EOF." )

   end
   fp:close()
   fp = io.open( filename, "w+" )
   for i = 1, #content do

fp:write( string.format( "%s\n", content[i] ) )

   end
   fp:close()

end</lang>

OCaml

<lang ocaml>let input_line_opt ic =

 try Some (input_line ic)
 with End_of_file -> None

let delete_lines filename start skip =

 if start < 1 || skip < 1 then
   invalid_arg "delete_lines";
 let tmp_file = filename ^ ".tmp" in
 let ic = open_in filename
 and oc = open_out tmp_file in
 let until = start + skip - 1 in
 let rec aux i =
   match input_line_opt ic with
   | Some line ->
       if i < start || i > until
       then (output_string oc line; output_char oc '\n');
       aux (succ i)
   | None ->
       close_in ic;
       close_out oc;
       Sys.remove filename;
       Sys.rename tmp_file filename
 in
 aux 1

let usage () =

 Printf.printf "Usage:\n%s <filename> <startline> <skipnumber>\n" Sys.argv.(0);
 exit 0

let () =

 if Array.length Sys.argv < 4 then usage ();
 delete_lines
   Sys.argv.(1) (int_of_string Sys.argv.(2)) (int_of_string Sys.argv.(3))</lang>
$ cal > cal.txt
$ cat cal.txt
    juillet 2011    
lu ma me je ve sa di
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
 
$ ocaml deleteLines.ml cal.txt 5 2
$ cat cal.txt
    juillet 2011    
lu ma me je ve sa di
             1  2  3
 4  5  6  7  8  9 10
25 26 27 28 29 30 31
 

Perl

The value of deleting certain lines from a file notwithstanding, here's how you'd normally do it in Perl (call with perl rmlines -from=3 -to=10 filename:<lang perl>#!/usr/bin/perl -n -s -i print unless $. >= $from && $. <= $to;</lang> If you want a backup file (maybe because deleting lines from a file in place is a pretty silly idea), change the -i in the first line to -i.bak, for example. <lang Perl>#!/usr/bin/perl -w use strict ; use Tie::File ;

sub deletelines {

  my $arguments = shift ;
  my( $file , $startfrom , $howmany ) = @{$arguments} ;
  tie my @lines , 'Tie::File' , $file or die "Can't find file $file!\n" ;
  my $nrecs = @lines ;
  if ( $startfrom > $nrecs ) {
     print "Error! Starting to delete lines past the end of the file!\n" ;
     return ;
  }
  if ( ( $startfrom + $howmany ) > $nrecs ) {
     print "Error! Trying to delete lines past the end of the file!\n" ;
     return ;
  }
  splice @lines , $startfrom - 1 , $howmany ;
  untie @lines ;

}

if ( @ARGV != 3 ) {

  print "Error! Invoke with deletelines <filename> <start> <skiplines> !\n" ;
  exit( 1 ) ;

}

deletelines( \@ARGV ) ;</lang>

PicoLisp

<lang PicoLisp>(de deleteLines (File Start Cnt)

  (let L (in File (make (until (eof) (link (line)))))
     (if (> (+ (dec 'Start) Cnt) (length L))
        (quit "Not enough lines")
        (out File
           (mapc prinl (cut Start 'L))
           (mapc prinl (nth L (inc Cnt))) ) ) ) )</lang>

PowerBASIC

<lang powerbasic>#DIM ALL

FUNCTION PBMAIN () AS LONG

   DIM filespec AS STRING, linein AS STRING
   DIM linecount AS LONG, L0 AS LONG, ok AS LONG
   filespec = DIR$(COMMAND$(1))
   WHILE LEN(filespec)
       linecount = 0: ok = 0
       OPEN filespec FOR INPUT AS 1
       OPEN filespec & ".tmp" FOR OUTPUT AS 2
       DO UNTIL EOF(1)
           LINE INPUT #1, linein
           INCR linecount
           IF VAL(COMMAND$(2)) <> linecount THEN
               PRINT #2, linein
           ELSE
               ok = -1
               FOR L0 = 2 TO VAL(COMMAND$(3))
                   IF EOF(1) THEN
                       ok = 1
                       PRINT "Tried to remove beyond the end of "; filespec
                       EXIT DO
                   END IF
                   LINE INPUT #1, linein
               NEXT
           END IF
       LOOP
       IF 0 = ok THEN
           PRINT "Lines to remove are beyond the end of "; filespec
       ELSEIF -1 = ok THEN
           PRINT filespec; ": done"
       END IF
       CLOSE
       filespec = DIR$
   WEND

END FUNCTION</lang>

Sample output:

E:\users\Erik\Desktop>rmvlns.exe ?.txt 500 100
Lines to remove are beyond the end of 0.txt
Tried to remove beyond the end of 1.txt
2.txt: done
3.txt: done
4.txt: done

Seed7

<lang seed7>$ include "seed7_05.s7i";

 include "osfiles.s7i";

const proc: removeLines (in string: fileName, in integer: start, in integer: count) is func

 local
   var file: inFile is STD_NULL;
   var file: outFile is STD_NULL;
   var integer: lineNumber is 1;
   var string: line is "";
 begin
   inFile := open(fileName, "r");
   outFile := open(fileName & ".tmp", "w");
   while hasNext(inFile) do
     line := getln(inFile);
     if lineNumber < start or lineNumber >= start + count then
       writeln(outFile, line);
     end if;
     incr(lineNumber);
   end while;
   close(inFile);
   close(outFile);
   removeFile(fileName);
   moveFile(fileName & ".tmp", fileName);
 end func;

const proc: main is func

 begin
   if length(argv(PROGRAM)) = 3 then
     removeLines(argv(PROGRAM)[1], integer parse (argv(PROGRAM)[2]), integer parse (argv(PROGRAM)[3]));
   end if;
 end func;</lang>

Tcl

<lang tcl>proc removeLines {fileName startLine count} {

   # Work out range to remove
   set from [expr {$startLine - 1}]
   set to [expr {$startLine + $count - 2}]
   # Read the lines
   set f [open $fileName]
   set lines [split [read $f] "\n"]
   close $f
   # Write the lines back out, without removed range
   set f [open $fileName w]
   puts -nonewline $f [join [lreplace $lines $from $to] "\n"]
   close $f

}</lang>