Solved

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

Posted on 2011-09-22
5
296 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

19 Experts available now in Live!

Get 1:1 Help Now