Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file, f1.txt:

ID     Name
1      a
2         b
3   g
6            f

The number of spaces is not fixed. What is the best way to replace all the white spaces with one space using only tr?

This is what I have so far:

cat f1.txt | tr -d " "

But the output is:

IDName
1a
2b
3g
6f

But I want it to look like this:

ID Name
1 a
2 b
3 g
6 f

Please try and avoid sed :)

share|improve this question
3  
Why is it so important to avoid sed? Use whatever works! –  David Richerby Jul 22 at 23:00
1  
Because I know how to do it with sed. Wanted to know other ways : ) –  Unknown Jul 22 at 23:07

3 Answers 3

up vote 14 down vote accepted

With tr, use the -s ("squeeze repeats") option:

$ tr -s " " < file
ID Name
1 a
2 b
3 g
6 f

Or you can use an awk solution:

$ awk '{$2=$2}1' file
ID Name
1 a
2 b
3 g
6 f

When you change a field in record, awk rebuild $0, takes all field and concat them together, separated by OFS, which is a space by default.

share|improve this answer
    
This is a great solution too . . . I dont know which one to choose now :/ @Gnouc –  Unknown Jul 22 at 19:11
    
Feel free to chose any solution that you like and it works for you. A note that my solution is different with @polym's answer. –  cuonglm Jul 22 at 19:13
    
I am going to choose @polym because he could use the reputation points :). Please remember both of your answers are equally good :) –  Unknown Jul 22 at 19:15
    
@Unknown: Didn't read your answer carefully. I added a tr solution. –  cuonglm Jul 22 at 19:17
    
:)) yay! @Gnouc s answer is really dynamic, because he uses awk, he can do anything. You can also accept his solution. Just one thing: Gnouc can you possibly explain what the awk format in your command does? Also can you add tabs/spaces so that the output is conforming to Unknown's expected output? –  polym Jul 22 at 19:17

Just use column:

column -t inputFile

Output:

ID  Name
1   a
2   b
3   g
6   f
share|improve this answer
    
Wonderful and a quick reply :) –  Unknown Jul 22 at 19:04
1  
@Unknown Great to be at service :)! –  polym Jul 22 at 19:05
1  
@Gnouc wow cool, column also takes a file as argument. nice thanks! –  polym Jul 22 at 19:05
    
How can I get the second column only if I want ? I tried column -t f1.txt | cut -d " " -f2 But was not a solution i expected –  Unknown Jul 22 at 19:08
1  
Use awk then: column -t file | awk '{print $2}' prints the second column only –  polym Jul 22 at 19:09

Who needs a program (other than the shell)?

while read a b
do
    echo "$a $b"
done < f1.txt

If you want the values in the second column to line up, as in polym’s column answer, use printf instead of echo:

while read a b
do
    printf '%-2s %s\n' "$a" "$b"
done < f1.txt
share|improve this answer
    
In the first place, when compared with tr - this is a terribly weak suggestion efficiency-wise unless the input is just too small too outweigh the tiny cost of tr's invocation - which is not to mention how much more work it takes to write. Last, wouldn't you say that this post does not actually answer the question as asked? What is the best way to replace all the white spaces with one space using only tr? –  mikeserv Jul 24 at 23:22
    
And besides - couldn't you more easily just do something with $IFS? Maybe like: IFS=' <tab>' set -f ; echo $(cat <file)? –  mikeserv Jul 24 at 23:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.