使用echo和cat的不需要的空行 [英] Unwanted empty lines using echo and cat
问题描述
我想在文本文件的开头添加一行。我有input.txt:
I want to add a line to the beginning of a text file. I have input.txt:
line1
line2
line3
,并想添加行 time / F:x1:x2以结束
and want to add the line 'time/F:x1:x2' to end up with
time/F:x1:x2
line1
line2
line3
我正在尝试使用
echo 'time/F:x1:x2' | cat - input.txt>output.txt
但我得到的是
time/F:x1:x2
line1
line2
line3
也就是说,我得到了不需要的空行。如何避免这种情况?
[edit:这与不需要的重复使用echo和猫,因为我错误地询问了那里的换行符-我真正要避免的是空行。]
that is, I get empty lines I don't want. How can I avoid this? [edit: This is not a duplicate of Unwanted line break using echo and cat since I wrongly asked about line breaks there - what I really want to avoid is empty lines.]
推荐答案
也许您的原始文件具有MS-DOS行尾( \r\n
),并且您用来显示文件的任何内容都足以检测到该错误。然后,当您在顶部添加以unix行结尾( \n
)的行时,检测将中断,并且会在中间显示空白行。
Perhaps your original file has MS-DOS line endings (\r\n
), and whatever you use to display the file is smart enough to detect that. Then, when you add a line with unix line ending (\n
) to the top, the detection breaks, and you get shown the blank lines inbetween.
编辑
有多种转换行尾的方法。大多数编辑者都可以交互式地为您完成此任务。例如,您可以使用
Edit There are myriads of ways to convert line endings. Most editors can do it for you interactively. In your case, you can for instance extend your pipeline with
| tr -d '\r' > output.txt
这篇关于使用echo和cat的不需要的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!