bash 使用tr删除多个文件中的换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26228060/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:29:07  来源:igfitidea点击:

Use tr to remove line breaks in multiple files?

bashfor-loopnewlinetrfile-manipulation

提问by Ted Maclin

I have a set of several hundred .txt files that I am analyzing (ngram analysis using NSP), and I need to remove all the line breaks from each file. I can do it one at a time using tr:

我正在分析一组数百​​个 .txt 文件(使用 NSP 进行 ngram 分析),我需要从每个文件中删除所有换行符。我可以使用 tr 一次完成一个:

$ tr -d "\n\r" < input1.txt > output1.txt

How can I do this for my entire directory of files at once?

如何一次为我的整个文件目录执行此操作?

回答by Nick Russo

This will add -out before .txt. You haven't specified what the filenames are like, other than .txt, so hopefully you don't have input files named foo-out.txt, etc.

这将在 .txt 之前添加 -out。除了 .txt 之外,您还没有指定文件名,所以希望您没有名为 foo-out.txt 等的输入文件。

for f in *.txt
do
  tr -d "\n\r" < "$f" > $(basename "$f" .txt)-out.txt
done