The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Trim inside awk subin_bala Shell Programming and Scripting 3 05-06-2008 08:51 AM
trim lines melanie_pfefer Shell Programming and Scripting 6 03-26-2008 07:25 AM
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 11:27 AM
trim letters fed.linuxgossip Shell Programming and Scripting 10 05-15-2007 10:11 PM
Trim JWilliams Shell Programming and Scripting 4 04-24-2007 02:52 AM

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

Join Date: Jul 2005
Posts: 19
trim fields using sed

Hello

I would like to trim any trailing spaces from a field before it is written to a file.

Here is an example of the output string:
Index ~Description~Keys ~Time ~

There can be any number of trailing spaces before the tilde

I tried to use sed, and it sort of worked - it got rid of the trailing spaces but, it also removed the space between the words in the Description, which I don't want.

Here is the sed command I used
sed -e 's/[ ]*//g' file1 > file2

How can I modify this to delete the trailing spaces but keep the spaces between words?

Any help would be greatly appreciated - Thanks!
  #2 (permalink)  
Old 04-04-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Try:
tr -s ' ' < input_file > output_file

Please note there is 1 (one) space between the single quotes.
  #3 (permalink)  
Old 04-04-2007
phystech846 phystech846 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 2
If you need to remove only spaces before tilde this one should work:

sed 's/[ ]*~/~/g' file1 >file2
  #4 (permalink)  
Old 02-23-2009
bithead2k bithead2k is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 1
Smile sed command to trim leading and trailing whitespace

sed 's/^[[:space:]]*\(.*\)[[:space:]]*$/\1/'
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

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 10:29 PM.


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