Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to use sed to remove all space characters from a text file. At present I am using this command:

sed 's/ //' test.txt > test2.txt

This works in the sense that it removes the first space character of each line but leaves the rest.

Is there a way to tell sed to repeat for a space characters on a line before moving on to the next or is it a case of scripting multiple run thoughs on the same file?

At present the file is small for testing but soon this will be ramped up on to much larger files so minimum rinse and repeat would be great. Any thoughts?

At present I get this using the command above:

bruteforce  
allthe kings men  
moneymoney money  
ifi was a rich girl

When what I would like is this:

bruteforce  
allthekingsmen  
moneymoneymoney  
ifiwasarichgirl  
share|improve this question

1 Answer 1

up vote 6 down vote accepted

You should use the g (global) flag, which makes sed act on all matches of the pattern on each line, rather than the first match on each line:

sed 's/ //g' test.txt > test2.txt
share|improve this answer
    
Excellent! What does the g do? Is it a repeat for all instance on a line switch or something? –  Tim Alexander Jul 25 '10 at 10:55
    
Yes, the g flag makes the regex match globally. Without it, it matches only the first one. –  jmz Jul 26 '10 at 8:18

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.