(2010-06-20) WRITE.PL - Write lines to a text file

This script will open a file to write one or more lines to it. It can either create a new file, or append to an existing file. The first parameter is the file name (a leading "+" indicates that it should append to the file, otherwise it will create a new file).

Each additional parameter is one line to be written to the file. Surrounding a parameter with quotes will allow it to contain multiple words.


#!/bin/perl # # Perl script to replace the functionality of WRITE # Written: 2010-06-20 # By: Rob Ewan # # Usage: # write.pl fileName "line1" "line2" ... # # If the fileName begins with "+", the output will be appended # # First parameter is the file name $filename = $ARGV[0]; $fileOpen = 0; # Open the output file (for write, or for append) if ($filename =~ /^\+/) { $filename =~ s/^\+//; if (open(OUTFILE, ">> $filename")) { $fileOpen = 1; } } else { if (open(OUTFILE, "> $filename")) { $fileOpen = 1; } } # If the open worked, if ($fileOpen) { # Write each additional parameter on a separate line for ($i = 1; $i < @ARGV; $i++) { print OUTFILE "$ARGV[$i]\n"; } close(OUTFILE); } else { print "Could not open file.\n" }
- Read the book on AWK.
- Read more of my ideas on programming.
- Look up some other scripts.
- Go back to the front gate.

Do you have a script which I might find useful? Did you find a problem in one of my scripts? Write to Rob to let me know.
Page maintained by Rob.