The UNIX and Linux Forums  

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
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 05:04 AM
Removing leading and trailing spaces of data between the tags in xml. jhmr7 UNIX for Dummies Questions & Answers 2 05-18-2005 11: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 Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-04-2009
kumar04 kumar04 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 6
Removing leading and trailing spaces only in PERL

Hi All,

I have a file with the following contents with multiple lines

Code:
172445957|   000005911|8| 400 Peninsula Ave.#1551  | And,K |935172445957|000005911
607573888  |000098536  | 2|Ane, B |J |Ane |1868 |19861206|20090106|20071001
I want to trim the "leading and trailing spaces only" from each of the field of each line in perl.

For example: I want to remove leading space from " 000005911" and leading and trailing spaces from " 400 Peninsula Ave.#1551 " within the | delimter, not in between spaces of 400 and Peninsula and Ave.

I tried the following code
Code:
$data =~ s/^\s/+/;
$data =~ s/\s+$//;
It removes all the spaces from each field of the line.i.e. it gives me the result like for a specific field

from
" 400 Peninsula Ave.#1551 "

to

"400PeninsulaAve.#1551"

But I want like

"400 Peninsula Ave.#1551" no leading and trailing spaces

I just the put quotes for explanation.

Please help me if I can do it perl.

Appreciate your help.

Regards

-Kumar

Last edited by Yogesh Sawant; 04-04-2009 at 04:39 PM.. Reason: added code tags
  #2 (permalink)  
Old 04-04-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,900
Option 1:
Code:
$data =~ s/\|\s+/g;
$data =~ s/\s+\|/g;
Option 2:
Code:
$data = join '|', map { s/\s+$/; s/^\s+/; } split /|/, $data;
  #3 (permalink)  
Old 04-04-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
option 2 might need a small correction:

Code:
 split /|/, $data;
the pipe should be escaped:

Code:
 split /\|/, $data;
both are good, I prefer option 1 myself.
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 11:21 AM.


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