Re: [R] Reading newlines with read.table?

From: Allan Engelhardt <>
Date: Fri, 04 Jun 2010 17:07:36 +0100

I ended up pre-processing the files outside of R using a script along the lines of

for f in *_table_extract_*.txt; do

     echo -n "Processing $f..."
     iconv -f "UTF-16LE" -t "UTF-8" $f | \
         tail -c +4 | \
         perl -l012 -015 -pe 's/\n//g' | \
         perl -ne 'print if (!m{\A \( \d+ \s row\(s\) \s affected \) \s* 
\z}ixms && !m{\A \s* \z}xms)' | \
         xz -7 > $o
     echo "done."


Ugly, but it worked for me. You can change the first perl regular expression to do different things with line terminating \n versus in-field \n characters but I just dropped them all. The tail command drops the byte-order-mark (which we do not need for utf-8) and the second perl command drops blanks and a stupid SQL tool output.

Thanks to Prof. Brian Ripley who, essentially, pointed out that with embedded linefeed characters my file was a binary file and not really a text file. Her Majesty's government respectfully begs to disagree [1] but that's the R definition so we'll use it on this list.


[1] Original data sets described at and downloaded from (hint: you'll need p7zip to unpack them on a Linux box).

On 04/06/10 14:49, Allan Engelhardt wrote:
> I have a text file that is UTF-16LE encoded with CRLF line endings and
> '@' as field separators that I want to read in R on a Linux system.
> Which would be fine as
> read.table("foo.txt", file.encoding = "UTF-16LE", sep = "@", ...)
> *except* that the data may contain the LF character which R treats as
> end-of-line and then barfs that there are too few elements on that line.
> Any suggestions for how to process this one efficiently in R? [...] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 04 Jun 2010 - 16:10:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Jun 2010 - 17:10:27 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive