The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
csh script for deleting extra spaces in text file hertingm Shell Programming and Scripting 2 03-12-2009 04:22 PM
Remove extra spaces in a line vikas027 Shell Programming and Scripting 6 10-10-2008 11:07 AM
removing extra spaces agarwal Shell Programming and Scripting 2 04-25-2008 05:54 AM
How can i remove spaces in between the fields in a file dsravan Shell Programming and Scripting 3 09-26-2006 05:11 PM
To remove the extra spaces in unix Sho UNIX for Dummies Questions & Answers 4 05-04-2006 06:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-16-2009
srilaxmi srilaxmi is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
remove extra spaces between fields

Hi,

I have a source file as mentioned below: I want to remove all the extra spaces between the fields.

a b--------|sa df-------|3232---|3
sf sa------|afs sdf-----|43-----|33
a b c------|adfsa dsf---|23-32|23

*Here '-' idicates spaces

Now, I want output as below:

a b|sa df|3232|3
sf sa|afs sdf|43|33
a b c|adfsa dsf|23-32|23

How can i achieve this?

Thanks,
Sri
  #2 (permalink)  
Old 03-16-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311

Code:
sed 's/[[:space:]]\{2,\}\|//g' infile
a b|sa df|3232|3
sf sa|afs sdf|43|33
a b c|adfsa dsf|23-32|23

  #3 (permalink)  
Old 03-16-2009
anand_bh anand_bh is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 7
Smile Use tr

cat filename | tr -s ' '

This should work. Replaces two or more more spaces with one space.
  #4 (permalink)  
Old 03-16-2009
srilaxmi srilaxmi is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
Hi,

Thanks for your quick reply,

but im getting output as below using SED.

a bsa df32323
sf saafs sdf4333
a b cadfsa dsf23-3223

and using "tr" iam able to delete extra spaces but getting one space(as mentioned below) after each field, which i dont want :

a b |sa df |3232 |3
sf sa |afs sdf |43 |33
a b c |adfsa dsf |23-32 |23

please let me know where iam mistaken?

Thanks,
Sri
  #5 (permalink)  
Old 03-16-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
The sed works without problems on my box as you can see. Maybe you show what you issued and what the error output is or try it with gsed.
  #6 (permalink)  
Old 03-16-2009
Goldorakk's Avatar
Goldorakk Goldorakk is offline
Registered User
  
 

Join Date: Feb 2009
Location: France
Posts: 43
Using xpg4' sed on Solaris with "sed 's/[[:space:]]\{2,\}\|//g'" gives for me

Code:
a bsa df32323
sf saafs sdf4333
a b cadfsa dsf23 32|23

Must use "sed 's/[[:space:]]\{2,\}\|/|/g'" (just add | before /g) gives the good result

Code:
a b|sa df|3232|3
sf sa|afs sdf|43|33
a b c|adfsa dsf|23 32|23

  #7 (permalink)  
Old 03-17-2009
srilaxmi srilaxmi is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
thanks

Thanks all,

Goldorakk, your solution is working fine.

Can you please explain the code. I am unable to understand this.


Thanks,
Sri
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0