Henry’s Tech Advice #6 - Deleting SpacesPosted on March 27th, 2007 12 comments
Frustrated writes: Henry. I have a problem with a text file. There are spaces at the beginning of most of the lines and I need to get rid of them. Can you suggest an easy way to do this?
Greetings Frustrated. You have certainly come to the right place, for I am Henry the Adequate, superhero, and my computer guru-ness is second only to that guy who empties the bins at the office. No, no. I know what you’re thinking, but the bin thing is just a hobby. Professionally he is a a damn fine butcher, and just last week bought a computer for the kids, so you can believe me when I say he knows a thing or two.
Anyway, thanks to my enormous superheroic brain, the answer to your dilemma is as follows, assuming the file in question is called “file.txt”:
If you are using Microsoft Windows
- Double click on the file “file.txt” to open it in Notepad. This is one of the best text editors ever to be included free with a Windows operating system.
- Click at the beginning of the first line you wish to edit. Click and drag from here to just before the first non-space character, then release the mouse button.
- Press the “Delete” key on your keyboard (the thing with lots and lots and lots of buttons on it).
- Notice how all of the highlighted spaces magically disappear with a single keystroke! Magnificent isn’t it.
- Repeat 2,3,4 until you reach the end of the file.
- Click on “File/Save” to save your changes.
- Click on the little “x” in the top right of Notepad to exit.
It really is that easy with Windows, thanks to the power of the graphical interface.
If you are using Macintosh
Well, no experienced Mac user would be asking such a question, so I am going to assume you are a bit of a newbie. Firstly I would like you to think about that Star Trek movie where Scotty is trying to use a twentieth century system. “Computer,” he says, then waits for a response. That other Star Trek guy hands him the mouse. Of course, thinks Scotty. He holds the mouse up to his mouth. “Computer.”
“Just use the keyboard,” suggests some person who is really quite unimportant so I will just move on without discussing him any further.
You see? Scotty’s problem was that he expected the computer to understand his spoken, plain English commands. Your problem, of course, is exactly the opposite - Macs really are that clever. Just pick up the mouse and explain what it is you would like to have done.
If you are using a Linux/Unix computer
sed -i ’s/^[ \t]*//’ file.txt
I am a really great computer guru guy. In fact I am so clever I even managed to bluff my way through the Macintosh section without anybody noticing I know nothing whatsoever about Apple computers.
Need help with computer stuff? Henry can help. I promise a timely and definitive response to any technical question asked in the comments here.
promise - vaguely suggest that something might happen, if you’re really really lucky.
timely - If it occurs at all it will be within the context of the space-time continuum.
definitive - My response may contain some definitions, such as these.
12 responses to “Henry’s Tech Advice #6 - Deleting Spaces”
Dear Henry… I’ve added your blog to YouMob. The traffic won’t show up on your site as they use frames but you should get more readers through the link. Keep hanging on down under. It’s spring here so you will be getting colder down there.
Hadn’t heard of that one. Thanks.
“Cold”, even by our standards, is still a month or more away.
Umm, is this the ’slightly more popular’ one?
…and who is ‘HENRY’… your alter ego?
Yes, that ^ would be me.
Yes, this is indeed the ’slightly more popular’ weblog, which is not saying much, really.
Henry is this superhero guy.
And welcome, ice-laden one.
http://bloggerschoiceawards.com/ Nominated Henry here.
Wow, thanks. Wonder if I’ll get a vote.
Or you can open the file in Notepad, select Edit>Replace (or hit Ctrl+R on the keyboard). In the ‘Find What’ box, enter two spaces, leave the ‘Replace With’ box blank. Click once on ‘Replace all”. Now, all of the extra spaces are gone, leaving single spaces between words. Click the ‘X’ in the upper right hand corner and when the computer asks you if you would like to save the file, click on ‘Yes’.
And WordPad is much more powerful than Notepad.
A worthless prize to anybody who can spot the flaw in John’s suggestion.
I’m not sure what that would be Ben. It works fine for me.
Hi John. Your solution works very well for deleting multiple spaces from anywhere in the document, however it does not solve the problem at hand, which is deleting all space from the beginning of each line.
Your solution fails because:
1. It may delete spaces not at the beginning of lines.
2. It will fail to delete a single space at the beginning of a line.