The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to delete a word from a file? ntgobinath Shell Programming and Scripting 4 06-04-2008 05:03 PM
Delete Multiple White Spaces eja UNIX for Dummies Questions & Answers 6 04-18-2008 07:34 AM
Retaining Spaces within a word RcR UNIX for Dummies Questions & Answers 0 09-10-2007 12:19 AM
Delete spaces in between fields guiguy Shell Programming and Scripting 12 03-31-2006 10:38 AM
delete white spaces DebianJ Shell Programming and Scripting 2 10-12-2005 02:30 PM

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 12-04-2007
sachin.gangadha sachin.gangadha is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 37
how to delete spaces around a word

suppose a line has one word
ex:
unix

how to delete space around that word?
  #2 (permalink)  
Old 12-04-2007
denn denn is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 96
in vi:
Code:
%s= ==g

to remove all spaces, leave the g off if you only want to remove the first occurance of a space on each line.

or


Code:
tr -d '\040' < originalfile > outputfile

  #3 (permalink)  
Old 12-04-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,789
One way:

Code:
 echo '     unix   ' | sed 's/^[ ]*//g;s/[ ]*$//g'

  #4 (permalink)  
Old 12-04-2007
sachin.gangadha sachin.gangadha is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 37
In oracle we have a function called trim. is there a similiar function in unix?
  #5 (permalink)  
Old 12-04-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by sachin.gangadha View Post
In oracle we have a function called trim. is there a similiar function in unix?
NOW there is:

Code:
#!/bin/ksh

str='  1 2   3  5  '

function trim {
   
   echo "${1}" | /usr/xpg4/bin/awk '{gsub("^[[:space:]]+|[[:space:]]+$", "", $0); printf("%s\n", $0)}'
}

echo "[${str}] -> [$(trim "${str}")]"

  #6 (permalink)  
Old 12-04-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165

Code:
cat file | while read s
do
     echo $s
done

  #7 (permalink)  
Old 12-04-2007
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
There are many ways to "skin that cat":


Code:
echo '    unix    ' | awk '{print$1}'
echo '    unix    ' | read NEWVALUE
NEWVALUE=$(echo '    unix    ')

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 08:04 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