Monday 14 March 2016

Remove all Whitespaces from a file in Mac OS X

Lets look into a simple task today - remove all whitespaces from a file in Unix / Mac OS X from the command line.

To do so, let's consider a small example.

Let's have simple text file named Space_File.txt withe the below content. You see it's a regular text file with words separated with spaces.

1.Here is a cute little tutorial for
2.trying out different things using
3.the Stream Editor or Sed in Unix.
4.The author of this tutorial is
5.none other than IroncladWriter
6.himself. The tutorial aims to
7.teach folks and educate how
8.they could unlock the power
9.of Unix to the fullest. Am posting
10.this on the blog IronCladZone
11.The best blog on the whole of
12.internet, where you can learn
13.technical stuff as well enjoy 
14.entertaining tidbits like movies,
15.music, television, fashion, food,
16.shopping, travel, trends etc.
17.Just sit back, relax and

18.ENJOY THE RIDE :)

Now, we want to remove all the whitespace between the words. So let's use the following command.

tr -d ' \d' <Space_File.txt >NoSpace_File.txt

In this we pass the Space_Text.txt as input and redirect the output to a new file - NoSpace_File.txt. Note the space before \d. Do not miss this space. I repeat, DO NOT miss this space. It has to be ' \d' and not '\d'. Got it? All rite so the resultant output is as expected with no spaces:

1.Hereisacutelittletutorialfor
2.tryingoutifferentthingsusing
3.theStreamEitororSeinUnix.
4.Theauthorofthistutorialis
5.noneotherthanIronclaWriter
6.himself.Thetutorialaimsto
7.teachfolksaneucatehow
8.theycoulunlockthepower
9.ofUnixtothefullest.Amposting
10.thisontheblogIronClaZone
11.Thebestblogonthewholeof
12.internet,whereyoucanlearn
13.technicalstuffaswellenjoy
14.entertainingtibitslikemovies,
15.music,television,fashion,foo,
16.shopping,travel,trensetc.
17.Justsitback,relaxan

18.ENJOYTHERIDE:)

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker