\r instead of
\n inserts a null character into the text. To get a newline, use
\r. When searching for a newline, you’d still use
\n, however. This asymmetry is due to the fact that
\r do slightly different things:
\n matches an end of line (newline), whereas
\r matches a carriage return. On the other hand, in substitutions
\n inserts a null character whereas
\r inserts a newline (more precisely, it’s treated as the input CR). Here’s a small, non-interactive example to illustrate this, using the Vim command line feature (in other words, you can copy and paste the following into a terminal to run it).
xxd shows a hexdump of the resulting file.
echo bar > test
(echo 'Before:'; xxd test) > output.txt
vim test '+s/b/\n/' '+s/a/\r/' +wq
(echo 'After:'; xxd test) >> output.txt
0000000: 6261 720a bar.
0000000: 000a 720a ..r.
In other words,
\n has inserted the byte 0x00 into the text;
\r has inserted the byte 0x0a.
tail -n +2 "$FILE"
-n x: Just print the last
tail -n 5 would give you the last 5 lines of the input. The
+ sign kind of inverts the argument and make
tail print anything but the first
tail -n +1 would print the whole file,
tail -n +2 everything but the first line, etc.
tail is much faster than
tail is also available on BSD and the
-n +2 flag is consistent across both tools. Check the FreeBSD or OS X man pages for more.
The BSD version can be much slower than
sed, though. I wonder how they managed that;
tail should just read a file line by line while
sed does pretty complex operations involving interpreting a script, applying regular expressions and the like.
Note: You may be tempted to use
# THIS WILL GIVE YOU AN EMPTY FILE!
tail -n +2 "$FILE" > "$FILE"
but this will give you an empty file. The reason is that the redirection (
>) happens before
tail is invoked by the shell:
- Shell truncates file
- Shell creates a new process for
- Shell redirects stdout of the
tail process to
tail reads from the now empty
If you want to remove the first line inside the file, you should use:
tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"
&& will make sure that the file doesn't get overwritten when there is a problem.
It's gross, because sed normally processes a line at a time:
This is nicer:
I chose to replace the newline with a space. tr can only replace by a single character (or delete with the -d option).
Flexible and simple:
Where " " is whatever you want in place of the newline.