Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Perl handling of CR/LF and CRLF together


ghost's Avatar
0 0

I was writing a script today in order to print out some data from a file. Maybe I'm retarded in picking perl to perform this job, but it was one of those times where I just picked a scripting language out of a hat and went with it. I hadn't scripted much in perl either so I figured I would give it a shot.

Anyway, the whole point of the script is to parse through a file line by line and perform a function based on that line. For debugging purposes, I simply printed the line with a carriage return(\n). When I started to run the script, it spat out a humungous, long line of text and then quit. Another additional piece of information is that the log file was created by another perl script I ran. This was run in windows, which by default uses the CR or carriage return. Given that I used the > operator at the command line instead using the OPEN function in perl to write to a log file on windows is the obvious reason this deviated from my expected result (unless I'm way off). The main reason I did that was pure laziness at the time. So, when I tried to parse through the file with perl on Linux, it spat out a huge line because all of the line breaks were CR and it was looking for LF to indicate the end of line.

I used sed to fix the line breaks (sed -e s/'\r'/'\n'/g disabled.txt).

The whole purpose of this script is to run a CLI tool with the line to delete users from our directory tree. The reasons behind this are incredibly convoluted so I won't go into them here, but the code I wrote can be seen below. I didn't know if it would hold any vested interest with the rest of the community considering that's probably documented somewhere, but I'm sure someone will run into it again. Just an interesting tidbit on how perl handle's those characters.

    #!/usr/bin/perl
    ####### Written By vienessewaltzer ############
    use strict;
    use warnings;
    use IO::File;
    use Text::CSV;

    #Set the log file to use
    my $file = "disabled.txt";
    my $test = "test.txt";  
   
    
    open (F, "<$file") or die ("Could not open $file!");
    open (T, ">$test") or die ("could not open $test!");
    
    while (my $line = <F>)
    {
	print $line, "\n";
	print T $line;
    }
	close(F);
	close(T);```

Also worth noting, when you write to a file with perl functions it adds CRLF together.

starofale's Avatar
Member
0 0

Thanks for this. I've never thought about cross-platform compatibility when programming before - I normally just use LFs everywhere. Could be useful in the future.

Just thought I should point out some things:

vienessewaltzer wrote: I simply printed the line with a carriage return(\n).CR = \r LF = \n

vienessewaltzer wrote: This was run in windows, which by default uses the CR or carriage return.Windows uses CRLF (\r\n)


ghost's Avatar
0 0

starofale wrote:

[quote]vienessewaltzer wrote: This was run in windows, which by default uses the CR or carriage return.Windows uses CRLF (\r\n)

[/quote]

Thanks for the correction. Actually, I went back and looked at my other script, and when I printed anything out in it, I used \r so that explains it.