another trim question using tr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting another trim question using tr
# 1  
Old 03-17-2009
another trim question using tr

Hi all,

I've been looking for how to eliminate blank spaces in a variable or strings. I've seen several ways, using sed, awk and even python. One of them is use 'tr' command, but it does not work as I expected: For example:
Code:
echo "     stuff      " | tr -s " "

leaves one space ahead and another behind. Why?
I also tried
Code:
echo "     stuff      " | tr -d " "

and it works successfully. However, all samples I've seen with "tr", use -s option (which I guess it means replace) rather than -d option.
I'd like to know why.

Thanks a lot.

Albert.
# 2  
Old 03-17-2009
Albert, u can use the below. Works on bash and ksh.

Code:
 
echo "     stuff      " | tr -d [:space:]

HTH,Smilie

Regards,

Praveen
# 3  
Old 03-17-2009
Thanks sunpraveen, although it is not exactly what I asked for Smilie. However, it does help Smilie!!!

Albert.
# 4  
Old 03-17-2009
-d Delete all occurrences of input characters that are specified
-s Replace instances of repeated characters with a single character

Delete vs replace by a single char.

Did you try "-ds" ? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to trim the zero's after decimal?

Hello all, I have an XML with below content from which i need to remove the trailing zeros, like 123.00 should be converted to 123 and 123.01200 to 123.012 Below is the sample excerpt data from XML file. My input file size could be approximately 5 GB or less. CURRENT:... (10 Replies)
Discussion started by: Ariean
10 Replies

2. Shell Programming and Scripting

Trim the data

i am passing below inputfils using while loop and ouput should be trim as below INPUT: src_SPS_d_Comment_Tran_File_20130417_001.dat src_SPS_d_Corp_Adv_Tran_File_20130417_001.dat src_SPS_d_Letter_Tran_File_20130417_001.dat src_SPS_d_Loan_Level_File_20130417_001.dat... (8 Replies)
Discussion started by: katakamvivek
8 Replies

3. Shell Programming and Scripting

Trim String

Hi, I wish to grep everything before the last "/bin" in the following string /opt/app/bin/app1/jdk150_07/bin/IA64N/java Desired output: "/opt/app/bin/app1/jdk150_07" Kindly help ... (2 Replies)
Discussion started by: mohtashims
2 Replies

4. Shell Programming and Scripting

Help with awk trim

I am trying to trim spaces for the fixed width file starting from location 129 and of length 20. I am expecting only around 40 records that will have length greater than 9. But i am getting around 4000 records. Please help me correct the following. nawk '{if (a=length(gsub(/... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. Shell Programming and Scripting

Trim Filename

Hi All, I have a file named as FAB1_600015_CONRAD.A0_7XYZ12345.000_LT-SWET.01_LTPA25L_20110622-161429_07_WFR12345_20110622-161429_20110712-125228.data.dis I want to generate a directory taking only the 7XYZ12345.000_WFR12345 The length and format of the Filename will be the same... (2 Replies)
Discussion started by: asheshrocky
2 Replies

6. Shell Programming and Scripting

Trim a new line

Okay, I am trying to make a bash script to get a certain domains IP address (my home ip). My home is on a DHCP lease from my ISP, so I cannot always trust the IP address to remain constant. This is what I have so far for it: alias ip-home="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 |... (5 Replies)
Discussion started by: tnanek
5 Replies

7. Shell Programming and Scripting

Trim issue

Hi All, I am using trim in my code.. ID_SA_SOURCE="`echo "$data" | cut -c17-34 | tr -s " "`" ID_SA_DEST="`echo "$data" | cut -c35-52 | tr -s " "`" echo"$ID_SA_SOURCE";"$ID_SA_DEST"; the output is 0608166896; 3001339; contains one whitespace between the two ..how can i remove that single... (3 Replies)
Discussion started by: scorpio
3 Replies

8. Shell Programming and Scripting

trim lines

I want to delete all lines from 1 till a certain line. how to do that without redirection to another file (tail solution)? thanx (6 Replies)
Discussion started by: melanie_pfefer
6 Replies

9. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies

10. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies
Login or Register to Ask a Question