?
Solved

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

Posted on 2011-09-22
5
Medium Priority
?
305 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 664 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 668 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 668 total points
ID: 36597286
tr -s " " filename > outputfile

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36898432
Great!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this article, we’ll look at how to deploy ProxySQL.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

777 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