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

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 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: 4 July 2018, 9:23 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 03-03-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 5 June 2018, 9:29 AM EDT
Location: The Netherlands
Posts: 7,747
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 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 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 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
👤 Login to reply

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 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 06:48 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?