cianius
cianius

Reputation: 2412

replace varying number of spaces in file with single space

I have a list of files that have columns separated by varying number of spaces. HOw can I sed or similar this so that each column is separated by a single space or tab?

I tried:

 sed 's/ \+ /\t/g' file > tmp
 sed "s/\ /\t/g" tmp > file

but R complained

line 526 did not have 11 elements

Upvotes: 0

Views: 107

Answers (4)

BMW
BMW

Reputation: 45343

awk will take care of whitespace directly and easily

awk '$1=$1' infile     # convert to single space

or

awk '$1=$1' OFS="\t" infile     # convert to single tab

If you have to use SED, The character class \s will match the whitespace characters tab and space

so your sed code can be fixed as

sed 's/\s\{1,\}/ /g' infile

or

sed 's/\s\{1,\}/\t/g' infile

if your sed support -r option,

sed -r 's/\s+/ /g' infile

or

sed -r 's/\s+/\t/g' infile

Upvotes: 0

repzero
repzero

Reputation: 8402

Let say you have the file named "raw_file" that have the text below inside

COLUMN1            COLUMN2         COLUMN3     COLUMN4
I      am   a boy
Hello word

I            see you
I am 5 years
How are    you?

You can use the command:

sed 's/ \{1,\}/ /g' '/root/Desktop/raw_file' |column -s ' ' -t > '/root/Desktop/my_new_file'

RESULTS WHEN OUTPUT TO A NEW FILE CALLED "my_new_file"

COLUMN1  COLUMN2  COLUMN3  COLUMN4
I        am       a        boy
Hello    word
I        see      you
I        am       5        years

RESULTS WHEN OUTPUT TO THE TERMINAL

Command used:

sed 's/ \{1,\}/ /g' '/root/Desktop/raw_file' |column -s ' ' -t 2>  /dev/null

Results:

COLUMN1  COLUMN2  COLUMN3  COLUMN4
I        am       a        boy
Hello    word
I        see      you
I        am       5        years

NOTE: Each column is separated by two spaces.

Upvotes: 0

amit_g
amit_g

Reputation: 31270

You could use tr

tr -s < fileName

or sed

sed -e 's/ \+/ /g' fileName

Inline sed

sed -i.bak -e 's/ \+/ /g' fileName

Upvotes: 3

economy
economy

Reputation: 4251

Try:

sed 's/ \{1,\}/\t/g' file > tmp

This takes one or more spaces and converts to the string in the second part of the expression ('\t' here).

Upvotes: 2

Related Questions