[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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

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
libertyforall2
Asked:
libertyforall2
3 Solutions
 
rbrideCommented:
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
 
ozoCommented:
perl -i.bak -lpe 's/\s+/ /g' sample.txt
0
 
TommySzalapskiCommented:
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
 
Kamaraj SubramanianApplication Support AnalystCommented:
tr -s " " filename > outputfile

Open in new window

0
 
libertyforall2Author Commented:
Great!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now