How to replace ^ M with a newline in perl

My test file has an "n" number of lines, and there is ^ M between each line, which in turn makes it one large line. The code I'm working with opens the specified file and needs to parse the header and then the subsequent lines, and then look for the directory path and file name. But since the file just ends up as a large line, it does not work correctly

#!/usr/bin/perl #use strict; #use warnings; open (DATA, "<file.txt") or die ("Unable to open file"); my $search_string = "Directory Path"; my $column_search = "Filename"; my $header = <DATA>; my @header_titles = split /\t/, $header; my $extract_col = 0; my $col_search = 0; for my $header_line (@header_titles) { last if $header_line =~ m/$search_string/; $extract_col++; } for my $header_line (@header_titles) { last if $header_line =~m/$column_search/; $col_search++; } print "Extracting column $extract_col $search_string\n"; while ( my $row = <DATA> ) { last unless $row =~ /\S/; chomp $row; my @cells = split /\t/, $row; $cells[74]=~s/:/\//g; $cells[$extract_col]= $cells[74] . $cells[$col_search]; print "$cells[$extract_col] \n"; } 

When I open the test file in VI, I used

 :%s/^M/\r/g 

and what removes ^ M, but how to do this in this perl program? When I tried the test program and inserted it s\^M/\r/g and wrote it in another file, she got a lot of Chinese characters.

+4
source share
5 answers

Before you start reading the file, set $/ to "\r" . By default, this setting is set to a line character, which is great for UNIX-style line endings and is almost normal for DOS-style line endings, but useless for the old Mac-style line endings that you see. You can also try mac2unix on your input file if one is installed.

For more information, find "INPUT_RECORD_SEPARATOR" in the perlvar man page .

+4
source

If mac2unix does not work for you, you can write your own mac2unix as a single-line Perl:

 perl -pi -e 'tr/\r/\n/' file.txt 

This will most likely fail if the file size is larger than virtual memory, although it reads the entire file in memory.

For completeness, let dos2unix also be:

 perl -pi -e 'tr/\r//d' file.txt 

and unix2dos:

 perl -pi -e 's/\n/\r\n/g' file.txt 
+4
source

This file originated from a Windows system? If so, try running the dos2unix command in the file before reading it. You can do this before calling the perl script or inside the script before reading it.

0
source

You might want to set $ \ (input separator) to ^ M at the beginning of your script, for example:

 $\ = "^M"; 
0
source

perl -MExtUtils :: Command -e dos2unix file

0
source

All Articles