4

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:

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

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

Nick Russo
  • 1,522
  • 10
  • 13
Ted Maclin
  • 43
  • 1
  • 1
  • 3

1 Answers1

13

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.

for f in *.txt
do
  tr -d "\n\r" < "$f" > $(basename "$f" .txt)-out.txt
done
chx
  • 11,270
  • 7
  • 55
  • 129
Nick Russo
  • 1,522
  • 10
  • 13
  • 2
    Perfect. Simple and direct. I know I'm not supposed to say "thanks" here, but since I don't have enough rep to +1 this, thanks. – Ted Maclin Oct 07 '14 at 03:41
  • 1
    @TedMaclin : Note that if you get more input files and want to re-run this script in the same directory it will re-process the `*-out.txt` files. But I guess that's ok, since they won't be modified, and `tr` is fairly fast. But I suggest giving the output files a different extension, so you can still use the simple `*.txt` glob for your input files. Or even better, put the output files into a separate directory. – PM 2Ring Oct 07 '14 at 12:22
  • Separate directory is a great idea. The code as written will generate foo-out-out.txt and foo-out-out-out.txt when run repeatedly... :p – Nick Russo Oct 07 '14 at 14:11
  • If i want to overwrite it in same filename means how can I achieve it. – Mangoski Apr 05 '16 at 10:48
  • It's easiest to create new files, check that they contain what you want, and then remove the originals. If those new files are in a new (sub)directory, they can have the same names as the originals, and then easily be moved into place. – Nick Russo Apr 06 '16 at 20:20
  • 1
    Hope your file is not called `input 1.txt`. Let me fix. – chx Jun 05 '16 at 21:55