6

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
1
  • In this special case without empty lines: awk '$1=$1' file
    – Cyrus
    Oct 5, 2019 at 19:20

1 Answer 1

6

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
3
  • @msndm if you have additional requirements, please edit your question to include them Oct 5, 2019 at 17:03
  • Often seen is awk '{$1=$1} 1' to rewrite the line. Oct 5, 2019 at 17:19
  • @glennjackman indeed... that's what I always used to use, I think it was someone on U&L who suggested the alternate... dunno Oct 5, 2019 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .