Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

remove trailing and leading spaces using tr command

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-03-2009
sureshg_sampat sureshg_sampat is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 28 July 2013, 1:34 PM EDT
Location: Pune, India
Posts: 71
Thanks: 0
Thanked 1 Time in 1 Post
remove trailing and leading spaces using tr command

Dear All,

can you please advice how do i remove trailing and leading spaces from a pipe-delimited file using "tr" command

the below cmd, i tried removed all spaces
tr -d ' '<s1.txt>s2.txt1

Many thx
Suresh
Sponsored Links
    #2  
Old Unix and Linux 03-03-2009
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 19 July 2017, 7:13 AM EDT
Location: Rossem, Tazenda
Posts: 1,213
Thanks: 7
Thanked 18 Times in 17 Posts
could you post some sample data from your pipe-delimited file?
Sponsored Links
    #3  
Old Unix and Linux 03-03-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Quote:
Originally Posted by sureshg_sampat View Post
Dear All,

can you please advice how do i remove trailing and leading spaces from a pipe-delimited file using "tr" command

the below cmd, i tried removed all spaces
tr -d ' '<s1.txt>s2.txt1

Many thx
Suresh
Something like this?


Code:
 sed 's/ | /|/g' s1.txt > s2.txt1

Regards
    #4  
Old Unix and Linux 03-03-2009
sureshg_sampat sureshg_sampat is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 28 July 2013, 1:34 PM EDT
Location: Pune, India
Posts: 71
Thanks: 0
Thanked 1 Time in 1 Post
Hi

Source file looks like this
cat s1.txt
3301|2009-02-01 00:00:00 |K|GGG|TR|2009-02-12 16:30:58 |ad |12

Desired Output
3301|2009-02-01 00:00:00||K|GGG|TR|2009-02-12 16:30:58||ad|12

Would appreciate if we have a command in "tr"

Regards,
Suresh
Sponsored Links
    #5  
Old Unix and Linux 03-04-2009
steven_TTG steven_TTG is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 10 May 2013, 11:19 PM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
echo "3301|2009-02-01 00:00:00 |K|GGG|TR|2009-02-12 16:30:58 |ad |12" |tr ' ' '|'
3301|2009-02-01|00:00:00||K|GGG|TR|2009-02-12|16:30:58||ad||12
Sponsored Links
    #6  
Old Unix and Linux 03-04-2009
rikxik's Unix or Linux Image
rikxik rikxik is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 October 2011, 2:20 AM EDT
Posts: 259
Thanks: 0
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by steven_TTG View Post
echo "3301|2009-02-01 00:00:00 |K|GGG|TR|2009-02-12 16:30:58 |ad |12" |tr ' ' '|'
3301|2009-02-01|00:00:00||K|GGG|TR|2009-02-12|16:30:58||ad||12
That does not match the desired output - you have put a | between date and time.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove Trailing spaces after a delimiter kiran_418 UNIX for Dummies Questions & Answers 1 04-29-2008 02:19 PM
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 01:24 PM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 10:48 PM



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