ajith.mk
ajith.mk

Reputation: 85

is newline treated as space in perl

consider this perl one liner

perl -e "$\=qq{\n};$/=qq{ };while(<>){print;}" "perl.txt" > perlMod.txt

contents of perl.txt are

a b
c

contents of perlMod.txt are

a
b
c

contents of perlMod.txt in hex are

61200D0A620D0A630D0A

Note that I have specified space as input record separator and "\n" as default output record separator. I am expecting two '0D0A's after b(62 in hex). One 0D0A is the new line after b. The other 0D0A belongs to output record separator. Why is there only one 0D0A.

Upvotes: 0

Views: 79

Answers (1)

ikegami
ikegami

Reputation: 385655

You seem to think <> will still stop reading at a linefeed even though you changed the input record separator.


Your input contains 61 20 62 0D 0A 63 or a␠b␍␊c.

The first read reads a␠.

You print a␠.
To that, $\ gets added, giving a␠␊.
Then :crlf does its translation, giving a␠␍␊.

There is no other spaces in the file, so your second read reads the rest of the file: b␍␊c.
Then :crlf does its translation, giving b␊c.

You print b␊c.
To that, $\ gets added, giving b␊c␊.
Then :crlf does its translation, giving b␍␊c␍␊.

So, altogether, you get a␠␍␊b␍␊c␍␊, or 61 20 0D 0A 62 0D 0A 63 0D 0A.

Upvotes: 6

Related Questions