0

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 

4 Answers 4

3

You could use tr

tr -s < fileName 

or sed

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

Inline sed

sed -i.bak -e 's/ \+/ /g' fileName 
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't have -s option in tr command, my tr version is tr (GNU coreutils) 8.4 running on centOS, can you share your tr version?
2

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).

2 Comments

+ 1 Welcome to the site. you could also specify "-i" for in place
Thanks! The -i option is useful, but be careful to supply the suffix required to create a backup. Easy to overwrite a single copy with -i alone.
0

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.

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.