Solved

delete empty spaces greater that one between each column in a text file using shell script

Posted on 2011-09-22
5
298 Views
Last Modified: 2012-05-12
I have the attached file. I need to have only one space separating columns but the format between some rows is messed up. How do I use shell do delete more than one space between columns?
sample.txt
0
Comment
Question by:libertyforall2
5 Comments
 
LVL 4

Assisted Solution

by:rbride
rbride earned 166 total points
ID: 36585402
If you are using normal UNIX shell tools (also cygwin) etc, you can just do the following:

cat sample.txt | sed 's/\s\s*/ /g' > output.txt

You can also do it with perl:
perl -n -e '{ s/\s\s*/ /g; print "$_\n"; }' < sample.txt > output.txt
0
 
LVL 84

Accepted Solution

by:
ozo earned 167 total points
ID: 36585420
perl -i.bak -lpe 's/\s+/ /g' sample.txt
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36586528
Basically in whatever language you use, there should be a find/replace. Replace any instance of two spaces with a single space. To also remove triple spaces, etc just keep doing the first step until it doesn't change anything.
0
 
LVL 23

Assisted Solution

by:Kamaraj Subramanian
Kamaraj Subramanian earned 167 total points
ID: 36597286
tr -s " " filename > outputfile

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36898432
Great!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now