How to get awk to edit in place and join all lines in text file

Hi,

I lack the utter fundamentals on how to craft an awk script.

I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many files, I need a batch process.

What would be ideal is:

awk -f ~/scripts/join_all_lines.awk *.txt

But I having trouble crafting "join_all_lines.awk".

I have found:

awk -vORS=' ' 1 filename

but I don't know how to script that into "join_all_lines.awk" and also it doesn't edit the file in place, it just displays the output.

I also found:

xargs echo <filename

it gives the same result. I don't know how to get them to edit a bunch of text files in place.

Can anyone help me out? Plz?

Hi

for i in *.txt;do paste -s -d" " $i > ${i}_ && mv -f ${i}_ $i;done

I would suggest to take a backup of your input files before trying the above command.

Guru.

1 Like

Yes!

Superb!

Many thanks to you!

You just saved me a lot of tedious labor.

for f in *.txt; do printf '1,$j\nw\nq\n' | ed -s "$f"; done

or

for f in *.txt; do ed -s "$f" <<'EOED'; done
1,$j
w
q
EOED