Inspired by this question: sed: N command does not read single line I wanted to understand why there appears to be an inconsistency between the following 2 commands:
#1. echo
This command produces no output
$ echo -en 'abc\n' | sed -n 'N;p'
$
A hex dump of the output from echo
:
od - octal dump
$ echo -en 'abc\n' | od -tx1
0000000 61 62 63 0a
0000004
hexdump
$ echo -en 'abc\n' | hexdump -C
00000000 61 62 63 0a |abc.|
00000004
#2. cat
If I put the string 'abc\n' in a file such as this:
1: abc
2:
That's a line with the string abc
on it followed by a linefeed (\n
aka. 0x0A
). Then pipe it to sed
as before, I get this:
$ cat abc.txt | sed -n 'N;p'
abc
$
That's output that includes the string abc
followed by 2 linefeeds.
od - octal dump
$ cat abc.txt | od -tx1
0000000 61 62 63 0a 0a
0000005
hexdump
$ cat abc.txt | hexdump -C
00000000 61 62 63 0a 0a |abc..|
00000005
I'm a little perplexed as to what's going on?
UPDATE
OK so my issue was with how I was creating the file abc.txt
as @choroba pointed out. I was creating the file in vim
and not realizing that it was adding 2 linefeeds. When I enabled visibility of special characters it became a little more obvious:
:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<,
:set list
Now in vim
file abc.txt
looked like this:
abc$
$
If I created the file as @choroba suggested then the file abc.txt
showed up as expected:
$ cat abc.txt | od -tx1
0000000 61 62 63 0a
0000004
And it behaved identical to the original echo
example:
$ cat abc.txt | sed -n 'N;p'
$
Original issue
My original issue with why sed -n 'N;p'
wasn't displaying anything was answered thanks to @enzotib's answer. The bit from the POSIX standard is what I wasn't picking up on:
If no next line of input is available, the N command verb shall branch to the end of the script and quit without starting a new cycle or copying the pattern space to standard output
hd
produces easier-to-read hex dumps thanod -x
. Or evenod -tx1
, although personally I prefer to see the characters where possible, as withhd
. At any rate, both of those will solve the endian issue.od
for 15+ years so it's hard to unlearn it 8-). I'll modify my Q to use the-tx1
switches as you've suggested though so it's easier to read.hd
is also known as hexdump.hexdump
(and various other bsd tools likecolumn
andcal
).hd
is the same ashexdump -C
.