Use tr to remove line breaks in multiple files?
Asked Answered
S

1

5

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?

Spruce answered 7/10, 2014 at 3:18 Comment(0)
A
14

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
Albertype answered 7/10, 2014 at 3:22 Comment(6)
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.Spruce
@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.Summertree
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... :pAlbertype
If i want to overwrite it in same filename means how can I achieve it.Othilie
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.Albertype
Hope your file is not called input 1.txt. Let me fix.Cocotte

© 2022 - 2024 — McMap. All rights reserved.