Solved

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

Posted on 2011-09-22
5
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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.
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 …

738 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