how to delete spaces around a word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to delete spaces around a word
# 1  
Old 12-04-2007
how to delete spaces around a word

suppose a line has one word
ex:
unix

how to delete space around that word?
# 2  
Old 12-04-2007
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  
Old 12-04-2007
One way:
Code:
 echo '     unix   ' | sed 's/^[ ]*//g;s/[ ]*$//g'

# 4  
Old 12-04-2007
In oracle we have a function called trim. is there a similiar function in unix?
# 5  
Old 12-04-2007
There are many ways to "skin that cat":

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

# 6  
Old 12-04-2007
Quote:
Originally Posted by sachin.gangadha
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}")]"

# 7  
Old 12-04-2007
Code:
cat file | while read s
do
     echo $s
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it?

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it. for example there is a file a.txt. $more a.txt create view as (select from ......... .......... ( select .... ( select ...... .. select only no ((( number ( select end (12 Replies)
Discussion started by: neelmani
12 Replies

2. Shell Programming and Scripting

No delete black spaces!

Hi, I have the next problem, i am triying to concatenate two variables with white spaces at the cornes, but the shell deletes them. For example i have the next code: A="Hello " B="Hello" echo $A$B output: Hello Hello You can see only one space between the words, and i put 5... (5 Replies)
Discussion started by: Xedrox
5 Replies

3. Shell Programming and Scripting

Prevent word splitting with file with spaces in name

Hello, I have a script that "validates" a ZIP file that look like this AAA_20120801.zip => x~back end~20120801.TXT y~time in~20120801.TXT z~heat_chamber~20120801.TXT AAA_20120801.ctlMy task is to compare its contents (i.e the list of files contained inside) with the control file that is... (2 Replies)
Discussion started by: alan
2 Replies

4. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

5. Ubuntu

How to remove multiple spaces in between word? (VI EDITOR)?

What last line mode command allows me to remove extra spaces in between words in a text? (1 Reply)
Discussion started by: rabeel
1 Replies

6. Shell Programming and Scripting

delete empty spaces at specific column

Hi All, If anybody could help me with my scenario here. I have a statement file. Example of some content: DATE DESC Debit Credit Total Rate 02-Jan-08 Lodgement 200.00 1200.00 2.51 14-Sep-07 Withdrawal 50.00 1000.00 ... (8 Replies)
Discussion started by: yonez
8 Replies

7. UNIX for Dummies Questions & Answers

Delete Multiple White Spaces

Hi, I have a file that has multiple spaces between characters. I want to delete or convert the multiple spaces into a single space. I think this can be done in "sed" but I only know the syntax to delete trailing or leading spaces. Can this be done with "sed" or awk? I have a file that looks... (6 Replies)
Discussion started by: eja
6 Replies

8. UNIX for Dummies Questions & Answers

Retaining Spaces within a word

Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces. Because of this field itself....it is taking almost three days to complete the file processing. I removed sed and... (0 Replies)
Discussion started by: RcR
0 Replies

9. Shell Programming and Scripting

Delete spaces in between fields

I am new to unix and need some assistance. I have a file in the format below with about 15 fields per each record. I have 2 records displayed below. "1234","Andy ","Rich ","0001","123 Main Street ","Dallas " "2345","Andrew ","Richter ","0002","234 First Ave ... (12 Replies)
Discussion started by: guiguy
12 Replies

10. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies
Login or Register to Ask a Question