0

I got about 300 (.txt) files in a folder with "normal text" formatted with /n's. I want all text in these files, to be in the first line, because a program in my pipeline requires that.

Exampel:

(IS:)
GTCGCAGCCG
TCGGCTCGGA
TCTCGGCCTC

(SHOULD BE:)
GTCGCAGCCGTCGGCTCGGATCTCGGCCTC

If I could overwrite them all, with file names staying the same, that would be convenient. I don't want to crack out python yet, is there an easy UNIX tool / command line approach?


I was here before: How do I remove newlines from a text file?

But how to do that for all 300 files in my folder? If i use tr -d '\n' < *.txt it tells me: "bash: *.txt: ambiguous redirect"

Aux
  • 15
  • 4
  • Does this answer your question? [How do I remove newlines from a text file?](https://stackoverflow.com/questions/3134791/how-do-i-remove-newlines-from-a-text-file) – Benjamin W. Nov 24 '21 at 16:04

1 Answers1

1

You need a shell loop to process each file in turn. Note that ed is a better choice than tr, as it is designed to work with files, not streams.

for f in *.txt; do
    printf '%%j\nwq\n' | ed "$f"
done

%j is the ed command to join all lines in the buffer; the %% is necessary here to make printf output a literal %. wq is the command to write changes back to the file and quite. (The q is optional, as ed will exit after the end of its script anyway.)

chepner
  • 497,756
  • 71
  • 530
  • 681