Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-04-2007
Registered User
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
how to delete spaces around a word

suppose a line has one word
ex:
unix

how to delete space around that word?
Sponsored Links
    #2  
Old 12-04-2007
Registered User
 
Join Date: Jul 2007
Posts: 116
Thanks: 0
Thanked 2 Times in 2 Posts
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

Sponsored Links
    #3  
Old 12-04-2007
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,645
Thanks: 164
Thanked 639 Times in 616 Posts
One way:

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

    #4  
Old 12-04-2007
Registered User
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
In oracle we have a function called trim. is there a similiar function in unix?
Sponsored Links
    #5  
Old 12-04-2007
Registered User
 
Join Date: Oct 2007
Posts: 160
Thanks: 0
Thanked 1 Time in 1 Post
There are many ways to "skin that cat":


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

Sponsored Links
    #6  
Old 12-04-2007
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,382
Thanks: 112
Thanked 486 Times in 458 Posts
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}")]"

Sponsored Links
    #7  
Old 12-04-2007
Read Only
 
Join Date: Nov 2007
Posts: 165
Thanks: 0
Thanked 2 Times in 2 Posts

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

Sponsored Links
Closed Thread

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search the word to be deleted and delete lines above this word starting from P1 to P3 vsachan Shell Programming and Scripting 2 05-24-2011 03:36 AM
How to remove multiple spaces in between word? (VI EDITOR)? rabeel Ubuntu 1 03-09-2011 02:40 AM
Retaining Spaces within a word RcR UNIX for Dummies Questions & Answers 0 09-09-2007 11:19 PM
Delete spaces in between fields guiguy Shell Programming and Scripting 12 03-31-2006 08:38 AM
delete white spaces DebianJ Shell Programming and Scripting 2 10-12-2005 01:30 PM



All times are GMT -4. The time now is 05:17 PM.