linux删除最后一个换行符,如果它是文件中的最后一个字符,如何删除换行符?...

这篇博客探讨了如何使用各种编程语言(如Perl、awk、Python、sed和bash)删除或检查文件末尾的换行符。文章提供了不同命令行工具的实例,包括 inplace 编辑和内存效率的考虑,适用于不同大小的文件。
摘要由CSDN通过智能技术生成

perl -pe 'chomp if eof' filename >filename2

或者,编辑文件到位:

perl -pi -e 'chomp if eof' filename

[编者注: -pi -e本来就是-pie ,但正如几位评论者所指出的那样,由@hvd解释,后者不起作用。]

这在我看到的awk网站上被形容为“perl blasphemy”。

但是,在一个testing中,它的工作。

您可以利用shell 命令replace删除结尾换行符的事实:

在bash,ksh,zsh中工作的简单forms:

printf %s "$(< in.txt)" > out.txt

便携式(符合POSIX)替代scheme(效率稍低):

printf %s "$(cat in.txt)" > out.txt

注意:

如果in.txt以多个换行符结尾,则命令replace将删除所有这些 – 谢谢@Sparhawk。 (它不会删除尾随换行符以外的空格字符。)

由于此方法将整个input文件读入内存 ,因此build议使用较小的文件。

printf %s确保没有新行被添加到输出(这是符合POSIX标准的非标准echo -n ;请参阅http://pubs.opengroup.org/onlinepubs/009696799/utilities/echo.html和https: //unix.stackexchange.com/a/65819 )

其他答案的指南 :

如果Perl是可用的,去接受的答案 – 它是简单的和高效的内存 (不会一次读取整个input文件)。

否则,考虑ghostdog74的awk答案 – 这是晦涩的,但也是内存效率 ; 一个更具可读性的等价物 (POSIX兼容)是:

awk 'NR > 1 { print prev } { prev=$0 } END { ORS=""; print }' in.txt

由于将输出logging分隔符( OFS )设置为空string,因此打印被延迟一行,因此最后一行可以在END块中处理,在该块中打印时没有尾随\n 。

如果你想要一个详尽而又快速和健壮的解决scheme来真正地进行编辑 (而不是创build一个临时文件,然后replace原来的文件),请考虑jrockway的Perl脚本 。

你可以用GNU coreutils来做这个,它支持相对于文件末尾的参数。 所以要放弃最后一个字节的使用:

head -c -1

要testing一个结束的换行符,你可以使用tail和wc 。 以下示例将结果保存到临时文件,然后覆盖原始文件:

if [[ $(tail -c1 file | wc -l) == 1 ]]; then head -c -1 file > file.tmp mv file.tmp file fi

您还可以使用moreutils sponge进行“就地”编辑:

[[ $(tail -c1 file | wc -l) == 1 ]] && head -c -1 file | sponge file

你也可以在你的.bashrc文件中填充一个通用的可重用函数:

# Example: remove-last-newline < multiline.txt function remove-last-newline(){ local file=$(mktemp) cat > $file if [[ $(tail -c1 $file | wc -l) == 1 ]]; then head -c -1 $file > $file.tmp mv $file.tmp $file fi cat $file }

head -n -1 abc > newfile tail -n 1 abc | tr -d '\n' >> newfile

编辑2:

这是一个awk版本(更正) ,不积累潜在的巨大数组:

awk'{if(line)print line; line = $ 0} END {printf $ 0}'abc

呆子

awk '{q=p;p=$0}NR>1{print q}END{ORS = ""; print p}' file

如果你想做的正确,你需要这样的事情:

use autodie qw(open sysseek sysread truncate); my $file = shift; open my $fh, '+>>', $file; my $pos = tell $fh; sysseek $fh, $pos - 1, 0; sysread $fh, my $buf, 1 or die 'No data to read?'; if($buf eq "\n"){ truncate $fh, $pos - 1; }

我们打开文件进行阅读和追加; 追加意味着我们已经seek了文件的结尾。 然后我们用tell来得到文件末尾的数字位置。 我们用这个数字来找回一个字符,然后我们读取这个字符。 如果是换行符,我们将文件截断到换行符之前的字符,否则,我们什么也不做。

这在任何input的恒定时间和恒定的空间运行,并且不需要更多的磁盘空间。

这是一个不错的,整洁的Python解决scheme。 我没有试图在这里简洁。

这会在原地修改文件,而不是复制文件并从副本的最后一行剥离换行符。 如果文件很大,这将比select最佳答案的Perl解决scheme快得多。

如果最后两个字节是CR / LF,它将截断一个文件两个字节,如果最后一个字节是LF,则截断一个文件。 如果最后一个字节不是(CR)LF,它不会尝试修改文件。 它处理错误。 在Python 2.6中testing

把它放在一个名为“striplast”和chmod +x striplast 。

#!/usr/bin/python # strip newline from last line of a file import sys def trunc(filename, new_len): try: # open with mode "append" so we have permission to modify # cannot open with mode "write" because that clobbers the file! f = open(filename, "ab") f.truncate(new_len) f.close() except IOError: print "cannot write to file:", filename sys.exit(2) # get input argument if len(sys.argv) == 2: filename = sys.argv[1] else: filename = "--help" # wrong number of arguments so print help if filename == "--help" or filename == "-h" or filename == "/?": print "Usage: %s " % sys.argv[0] print "Strips a newline off the last line of a file." sys.exit(1) try: # must have mode "b" (binary) to allow f.seek() with negative offset f = open(filename, "rb") except IOError: print "file does not exist:", filename sys.exit(2) SEEK_EOF = 2 f.seek(-2, SEEK_EOF) # seek to two bytes before end of file end_pos = f.tell() line = f.read() f.close() if line.endswith("\r\n"): trunc(filename, end_pos) elif line.endswith("\n"): trunc(filename, end_pos + 1)

PS本着“Perl高尔夫”的精神,这里是我最短的Python解决scheme。 它将整个文件从标准input缓冲到内存中,并将所有换行清除,并将结果写入标准输出。 不像Perl那样简洁; 你只是不能打败Perl这样的小棘手快速的东西。

从调用.rstrip()删除“\ n”,它将从文件末尾剥离所有空白,包括多个空白行。

把它放到“slurp_and_chomp.py”中,然后运行python slurp_and_chomp.py < inputfile > outputfile 。

import sys sys.stdout.write(sys.stdin.read().rstrip("\n"))

还有另外一个WTDI:

perl -i -p0777we's/\n\z//' filename

$ perl -e'local $ /; $ _ = <>; S / \ n $ //; 打印'a-text-file.txt

另请参阅在sed中匹配任何字符(包括换行符) 。

一个非常简单的单行文件的方法,需要coreutils的GNU回显:

/bin/echo -n $(cat $file)

使用dd:

file='/path/to/file' [[ "$(tail -c 1 "${file}" | tr -dc '\n' | wc -c)" -eq 1 ]] && \ printf "" | dd of="${file}" seek=$(($(stat -f "%z" "${file}") - 1)) bs=1 count=1 #printf "" | dd of="${file}" seek=$(($(wc -c < "${file}") - 1)) bs=1 count=1

perl -pi -e 's/\n$// if(eof)' your_file

假设Unix文件types,你只想要最后一个换行符。

sed -e '${/^$/d}'

它不会在多个换行符上工作…

* 仅在最后一行是空白行时有效。

一个快速的解决scheme是使用gnu实用程序截断:

[ -z $(tail -c1 file) ] && truncate -s-1

如果文件有新的尾行,则testing结果为真。

删除速度非常快,真正到位,不需要新文件,search也从最后一个字节读取(tail -c1)。

还有另外一个回答FTR(和我最喜欢的!):echo / cat你想要通过反引号去除和捕获输出。 最后的换行符将被删除。 例如:

# Sadly, outputs newline, and we have to feed the newline to sed to be portable echo thingy | sed -e 's/thing/sill/' # No newline! Happy. out=`echo thingy | sed -e 's/thing/sill/'` printf %s "$out" # Similarly for files: file=`cat file_ending_in_newline` printf %s "$file" > file_no_newline

我唯一想做的就是代码高尔夫,然后我把代码从文件中拷贝出来并粘贴到echo -n 'content'>file语句中。

sed ':a;/^\n*$/{$d;N;};/\n$/ba' file

我有一个类似的问题,但正在与Windows文件,并需要保持这些CRLF – 我的解决scheme在Linux上:

sed 's/\r//g' orig | awk '{if (NR>1) printf("\r\n"); printf("%s",$0)}' > tweaked

sed -n "1 x;1 !H $ {x;s/\n*$//p;} " YourFile

应该删除文件中最后发生的\ n。 无法处理大文件(由于sed缓冲区限制)

ruby:

ruby -ne 'print $stdin.eof ? $_.strip : $_'

要么:

ruby -ane 'q=p;p=$_;puts q if $.>1;END{print p.strip!}'

POSIX SED:

'$ {/ ^ $ / d}'

$ - match last line { COMMANDS } - A group of commands may be enclosed between { and } characters. This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match.

查看一个文件换行符是LF还是CRLF可以通过多种方式,具体方法取决于你使用的操作系统和可用的工具。以下是一些常见的方法: 1. 在Windows系统,你可以使用命令行工具如`type`命令,然后通过`findstr`命令的`/n`参数来查看文件的行号,行号之间的分隔符可以显示是LF还是CRLF。例如,你可以打开命令提示符并输入以下命令: ``` type 文件名.txt | findstr /n /r /c:"^" 文件名.txt ``` 这将输出文件内容,并在每行的开始显示行号。如果行号之间是LF,则表示文件使用LF换行符;如果是CR LF,则表示使用CRLF换行符。 2. 在Linux或macOS系统,你可以使用`cat`命令的`-vE`参数,它会显示文本文件的非打印字符,包括换行符。例如: ``` cat -vE 文件名.txt ``` 如果你看到行尾显示`$`符号,表示该行结束于LF;如果显示`^M$`(其`^M`是由Ctrl+V和Ctrl+M输入的),则表示该行结束于CRLF。 3. 使用文本编辑器或IDE(如Visual Studio Code、Sublime Text、IntelliJ IDEA等)打开文件,这些工具通常会提供查看和编辑文件换行符的功能,并且可以直观地显示文件内容。 4. 使用十六进制编辑器或查看器(如Notepad++、HxD等),可以直接查看文件的字节数据,LF换行符在十六进制表示为0x0A,而CRLF则是0x0D后跟0x0A。 5. 如果你熟悉命令行工具,可以使用`od`(八进制转储)命令在Linux或macOS系统查看文件的字节表示: ``` od -t x1 文件名.txt ``` 这个命令会以十六进制形式显示文件内容,你可以查找连续的0A(表示LF)或0D 0A(表示CRLF)来判断换行符类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值