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
How to remove trailing spaces mahek_bedi UNIX for Dummies Questions & Answers 2 08-10-2007 07:21 AM
Removing leading and trailing spaces of data between the tags in xml. jhmr7 UNIX for Dummies Questions & Answers 2 05-18-2005 10:27 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 11:48 PM
removing trailing spaces of a particular column in a file rooh UNIX for Dummies Questions & Answers 2 01-12-2002 11:34 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 03-28-2006
djkane djkane is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 27
Removing trailing spaces from delimited files

Hi All

I have a file of the following format (delimited by |)

this is field 1 | field 2 (lots of blank spaces) | field 3 (lots of blank space) |
field 1 | more text (lots of blank spaces) | dhjdsk |

Is there a way I can remove trailing spaces between the end of the text in each field and the delimiter? E.g. I would like to end up with the following...


this is field 1 | field 2 | field 3 |
field 1 | more text | dhjdsk |

Is this possible?
  #2 (permalink)  
Old 03-28-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
this is field 1 | field 2 (lots of blank spaces) | field 3 (lots of blank space) |
field 1 | more text (lots of blank spaces) | dhjdsk |
i could not spot any difference bet i/p and o/p
Quote:
this is field 1 | field 2 | field 3 |
field 1 | more text | dhjdsk |
is this what you are looking for
Code:
echo "a  | b  | c |" | sed "s/[' ']*|/|/g"
o/p: a| b| c|
  #3 (permalink)  
Old 03-28-2006
djkane djkane is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 27
works like a charm.... thanks mate
  #4 (permalink)  
Old 03-29-2006
djkane djkane is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 27
sed -e "s/[' ']*|/|/g" $FILE

Using the line of code above, i get trailing spaces to be removed between the end of the field and the delimiter. I was hoping someone might explain how the command knows to take from the last space.

For example, if i have a field a row in $FILE such as...

this is field 1 | this is field 2 | a lot of text in field 3

...how does the command know to remove the space after the "1" instead of taking everything after the first space, i.e., after "this".

Any explanation would be hugely appreciated.
  #5 (permalink)  
Old 03-29-2006
shriashishpatil shriashishpatil is offline
Registered User
  
 

Join Date: Dec 2005
Location: Pune
Posts: 81
try to concentrate on regex "[' ']*|". It means zero or more spaces followes by bar "|".

"[' ']*|" is replaced by "|".

hope its clear now.
  #6 (permalink)  
Old 03-29-2006
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Quote:
Originally Posted by djkane
sed -e "s/[' ']*|/|/g" $FILE.
[' ']*| Means all blanks ' ' before | , sed changes that for |.
You can see it better with an example:
Code:
echo "a        ssdddddddd| b       | c |" | sed "s/[d]*|/ |/g"
OUPUT:
a        ss | b        | c  |
Regards.
  #7 (permalink)  
Old 03-29-2006
djkane djkane is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 27
thanks guys for your replies. this has helped me to understand
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 12:37 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