删除最后一个空行
我的 .csv 文件末尾有一个分段符。我尝试使用以下命令删除文件末尾的空行。
sed -i '/^$/d' combined.csv
但它不起作用并且空白行仍然存在。我可以使用以下命令删除最后一行。
sed -i '$d' combined.csv
但是在删除最后一行之前是否可以检查它是否真的为空?
更新:
我使用以下命令来检查每行是否以数字开头。
sed -i '1s/^[^0-9]*//' combined.csv
这仅检查第一行,而不检查其余行。如何让它检查文件中的所有行?这可能会解决我的问题。
There is a para break at the end of my .csv file. I tried to remove the blank line that is at the end of the file using the following command.
sed -i '/^$/d' combined.csv
But it does not work and a blank line is still there. I can remove the last line using the following command.
sed -i '$d' combined.csv
But is it possible to check if the last line is really empty before removing it?
Update:
I am using the following command to check if each line start with a number.
sed -i '1s/^[^0-9]*//' combined.csv
This checks only for the first line and not the rest of the lines. How do I make it check all the lines in the file? This might solve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
${/^$/d;}
如果它是文件的最后一行,它只会匹配空行。更新:对于第二个问题,只需删除 s 之前的 1,即: sed -i 's/^[^0-9]*//'combined.csv
Try
${/^$/d;}
this will only match an empty line if it is the last line of the file.Update: for your second question, just remove the 1 before the s, i.e.:
sed -i 's/^[^0-9]*//' combined.csv
很久以前在某个地方发现了这个并保存了该片段。不要问我它是如何工作的:
Found this ages ago somewhere and saved the snippet. Do not ask me how it works:
我用 sed (GNU sed) 4.2.2 尝试过,并删除了所有空行,而不仅仅是空行(如果它是文件的最后一行)。
我发现以下命令对我自己有效,可以完成这项工作。
该命令来自有用的 SED-Oneliner 集合:
http://sed.sourceforge.net/sed1line.txt
I tried it with sed (GNU sed) 4.2.2 and got all blank lines deleted not only the empty line if it is the last line of the file.
I found the following Command, that worked for myself that does the Job.
This Command is from a Collection of useful SED-Oneliners:
http://sed.sourceforge.net/sed1line.txt
要删除空行,您可以使用
grep .
或sed '/^$/d'
它将删除文件中的任何空行。我希望您的文件中间没有任何空行,但这适用于您的情况。
cat 组合.csv | grep .
或
cat合并.csv | sed '/^$/d'
To remove blank lines you can use
grep .
orsed '/^$/d'
It will remove any blank line in the file. I hope you file does not have any blank lines in the middle but this will work in your case.
cat combined.csv | grep .
or
cat combined.csv | sed '/^$/d'
如果您确定最后一行是空,那么只需使用:
...|头 -n -1 | ...
If you know for sure that last line is empty, then just use:
...| head -n -1 | ...