#!/sw/bin/python # The "pound-bang" hack. Edit to put in # your system's full path to python. # Type "which python" at the command line # to find out the path, if you don't know it. # # Do a "chmod u+x" on this script to # make it executable, then you will be # able to execute it by just typing # it's name at the command line. # # # This program reads a text file, and alters the # newline tag as follows: # If it is a Mac file (\r) it turns it to Unix (\n) # If it is a Unix file (\n) it turns it to Mac (\r) # If it is DOS, (\r\n) it turns it to Unix # If it has a mixture of \r and \n it transforms to Unix # # The following import allows us to get at command line arguments # sys.argv is a list of the arguments entered on the command line, # with sys.argv[0] always being the script name, even if the # user entered no arguments. # import sys # Check if a file was specified on the command line if len(sys.argv) <=1: InFileName = raw_input("Please specify a file to process:") # This illustrates the use of "raw_input()" to prompt # the user to enter some data. It is done here only if # the user didn't enter an input file name on the command # line. else: InFileName = sys.argv[1] # # Build the output file name, or take it from the # second argument, if it's there. # if len(sys.argv) >=3: OutFileName = sys.argv[2] else: OutFileName = InFileName+".crfix" # Note: Should do something smarter with retaining the # extension of the infile name, if it's there. # # Open the input file and read the whole thing into a buffer # If it's a big file, you might want to replace this with # a loop, reading in and processing one line at a time using # readline() instead. # infile = open(InFileName,"r") buff = infile.read() infile.close() # # # Now check what kind of newline character is used, # and process the file accordingly. Here we use # buff.find(...) which returns the index of the first # occurrence of the argument, or -1 if the argument # doesn't occur in the string. Note that the string # buff is an object, and find is a method belonging # to it. # Instead of find(...), we could use count(...) which # counts the number of occurences. Note also it might # be nice to check to make sure that the newline character # is used consistently throughout (i.e. no mixed Mac and Unix # newlines). # # Note that replace(...) returns a new string which # is transformed as indicated. Below, we just overwrite the # old string with the new string, but you could save the # old one if you need to. if buff.find('\r\n') > -1 : buff = buff.replace('\r\n','\n') #Turn DOS to unix print "DOS file changed to Unix file ",OutFileName elif buff.find('\r') > -1 : buff = buff.replace('\r','\n') print "Mac file changed to Unix file ",OutFileName elif buff.find('\n') > -1 : buff = buff.replace('\n','\r') print "Unix file changed to Mac file ",OutFileName # # The file has been transformed. Now write it out # (to be completely safe, we ought to check if # OutFileName already exists, and ask first before # overwriting if it does) # outfile = open(OutFileName,"w") outfile.write(buff) outfile.close()