Removing leading and trailing spaces only in PERL | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Removing leading and trailing spaces only in PERL

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-04-2009
kumar04 kumar04 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 31 July 2009, 1:11 PM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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 03:39 PM.. Reason: added code tags
Sponsored Links
    #2  
Old 04-04-2009
pludi's Avatar
pludi pludi is offline Forum Advisor  
Cat herder
 
Join Date: Dec 2008
Last Activity: 28 March 2014, 8:35 AM EDT
Location: Vienna, Austria, Earth
Posts: 5,522
Thanks: 38
Thanked 335 Times in 308 Posts
Option 1:
Code:
$data =~ s/\|\s+/g;
$data =~ s/\s+\|/g;

Option 2:
Code:
$data = join '|', map { s/\s+$/; s/^\s+/; } split /|/, $data;

Sponsored Links
    #3  
Old 04-04-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
 
Join Date: Jan 2008
Last Activity: 11 September 2009, 3:44 PM EDT
Posts: 729
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
Closed Thread

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
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 04:04 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 01:24 PM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 10:48 PM
removing trailing spaces of a particular column in a file rooh UNIX for Dummies Questions & Answers 2 01-12-2002 10:34 AM



All times are GMT -4. The time now is 06:16 AM.