bash 删除文件末尾的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16365155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing a newline character at the end of a file
提问by Christopher Neylan
To remove all newlines you could, say:
要删除所有可能的换行符,请说:
tr -d '\n' < days.txt
cat days.txt | tr -d '\n'
but how would you use tr
to remove just the newline at the end/bottom of a text file?
但是您将如何使用tr
仅删除文本文件末尾/底部的换行符?
I'm not sure to specify just the last one.
我不确定只指定最后一个。
回答by Christophe Quintard
A simpler solution than the accepted one :
一种比公认的更简单的解决方案:
truncate -s -1 <<file>>
From the truncate man page :
从截断手册页:
-s, --size=SIZE
set or adjust the file size by SIZE
SIZE may also be prefixed by one of the following modifying characters:
'+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down
to multiple of, '%' round up to multiple of.
回答by Christopher Neylan
Take advantage of the fact that a) the newline character is at the end of the file and b) the character is 1 byte large: use the truncate
command to shrink the file by one byte:
利用 a) 换行符位于文件末尾和 b) 字符大小为 1 个字节的事实:使用truncate
命令将文件缩小一个字节:
# a file with the word "test" in it, with a newline at the end (5 characters total)
$ cat foo
test
# a hex dump of foo shows the '\n' at the end (0a)
$ xxd -p foo
746573740a
# and `stat` tells us the size of the file: 5 bytes (one for each character)
$ stat -c '%s' foo
5
# so we can use `truncate` to set the file size to 4 bytes instead
$ truncate -s 4 foo
# which will remove the newline at the end
$ xxd -p foo
74657374
$ cat foo
test$
You can also roll the sizing and math into a one line command:
您还可以将大小和数学转换为一行命令:
truncate -s $(($(stat -c '%s' foo)-1)) foo
回答by Raúl Salinas-Monteagudo
If you are sure the last character is a new-line, it is very simple:
如果您确定最后一个字符是换行符,则非常简单:
head -c -1 days.txt
head -c -N
means everything except for the last N bytes
head -c -N
表示除最后 N 个字节之外的所有内容
回答by Mark Reed
I think your best bet is Perl:
我认为你最好的选择是 Perl:
perl -0pe 's/\n\Z//' days.txt
The -0
causes perl to treat the whole file as one big string. The -p
tells it to print that string back out after running the program on it. And the -e
says "here's the program to run".
的-0
原因,Perl来处理整个文件作为一个大的字符串。该-p
告诉它打印字符串返回出来就可以运行该程序后。并-e
说“这是要运行的程序”。
The regular expression \n\Z
matches a newline, but only if it's the last character in a string. And s/\n\Z//
says to replace such a newline with nothing at all, deleting it.
正则表达式\n\Z
匹配换行符,但前提是它是字符串中的最后一个字符。并s/\n\Z//
说用什么都不替换这样的换行符,删除它。
The above command outputs the new version of the file, but you can instead modify the existing one by adding the -i
("in-place") option, optionally with a suffix that will be used to name a backup copy of the file before modifying it:
上面的命令输出文件的新版本,但您可以通过添加-i
("in-place") 选项来修改现有的文件,可选后缀将用于在修改文件之前命名文件的备份副本:
perl -i.bak -0pe 's/\n\Z//' days.txt
This solution is safe in that if the last character is nota newline, it won't be touched. The other solutions which simply remove the last byte no matter what may corrupt such a file.
此解决方案是安全的,因为如果最后一个字符不是换行符,则不会触及它。其他解决方案只是删除最后一个字节,无论什么可能破坏这样的文件。
回答by Lynch
Try this command:
sed '$ { /^$/ d}' days.txt
试试这个命令:
sed '$ { /^$/ d}' days.txt
You can read it as: "check if last line is an empty line. if so delete this line". I tested with both cases: first with a file having a new line at the end and an other time with a file ending with something else.
您可以将其读作:“检查最后一行是否为空行。如果是,请删除此行”。我对这两种情况进行了测试:首先是一个文件末尾有一个新行,另一个是文件以其他内容结尾。