Ubuntu – How to organize the disordered columns using awk/sed

awkcommand linesed

I have 2 columns including some data like:

one      two
one two
one    two

How can we convert it to:

one two
one two
one two

Best Answer

Awk - re-write the fields with the default (single space) output field separator:

$ awk '{NF+=0} 1' data
one two
one two
one two

Sed - substitute multiple spaces with single space:

$ sed 's/  */ /' data
one two
one two
one two

tr - squeeze (-s) spaces:

$ tr -s ' ' < data
one two
one two
one two

column:

$ column -t < data
one  two
one  two
one  two

rs (reshape) to two columns:

$ rs 0 2 < data
one  two
one  two
one  two