Register
It is currently Thu Apr 24, 2014 8:01 am

Translating tabs to spaces


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sun Apr 25, 2010 8:50 pm   

Joined: Sun Apr 25, 2010 8:34 pm
Posts: 3
Hello, I am new. I looked around the forums but couldn't find anything specific with the problem i'm having.

Purpose: To translate all 'tabs' in the input to 'three spaces'.

Code:
Here is my script (so far):
#!/bin/sh
input=`echo $* | tr '\t' '   '`
echo "$input"


The problem I'm having: The translate utility seems to only replace the '\t' with the first character in the second string.

So let’s say I give the script this input:
{tab}hello

I want
hello

I get
hello

Other things I have tried
Code:
#!/bin/sh
input=`echo $* | tr '\t' '\   '`
echo "$input"


Code:
#!/bin/sh
input=`echo $* | tr '\t' '\ \ \ '`
echo "$input"


Code:
#!/bin/sh
input=`echo $* | tr '\t' '"   "'`
echo "$input"


Nothing has worked so far, rather frustrating.


If you have read this far, you will most likely continue reading this, please do.
Thank you for taking your time to read this. Although you may find my script
to be amateur, I have just started in the world of unix/linux.If you have a solution
an explanation of why my script didn't work and an explanation of how the fixed version
works would be most helpful. I hope I didn't post in the wrong thread, my apologies.
thanks again.
Adam


Top
 Profile  
 PostPosted: Sun Apr 25, 2010 9:22 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
tr changes one char to one othe char.

bash can use Parameter Expansion:
Code:
#!/bin/bash

input="$1"
echo "${input/    /---}"
I changed spaces to dashes to be more visible.
but sh doesn't know this one; then you'll have to use sed


Top
 Profile  
 PostPosted: Sun Apr 25, 2010 9:42 pm   

Joined: Sun Apr 25, 2010 8:34 pm
Posts: 3
thank you, I will look into sed.


Top
 Profile  
 PostPosted: Mon Apr 26, 2010 3:11 pm   

Joined: Sun Apr 25, 2010 8:34 pm
Posts: 3
I got it to work.

Code:
#!/bin/sh
input=`cat $* | sed 's/{tab}/   /g'`
echo "$input"

where {tab} = a tab


Thank-you!

Does anyone know why \t doesn't work in sed? I can't seem to find that information.


Top
 Profile  
 PostPosted: Mon Apr 26, 2010 11:08 pm   

Joined: Wed Apr 14, 2010 10:23 am
Posts: 17
Because it's not required to (POSIX) and yours doesn't do it as additional feature (your documentation!).

To encode a literal tab, you can use Bash's (Bash! You use a SH, which might not be a Bash) ANSI-C like strings: http://wiki.bash-hackers.org/syntax/quo ... ke_strings

Code:
$ printf "test\ttext\n" | sed $'s/\t/---/'
test---text


Top
 Profile WWW  
 PostPosted: Tue Apr 27, 2010 2:36 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
TheBonsai wrote:
Because it's not required to (POSIX) and yours doesn't do it as additional feature (your documentation!).

To encode a literal tab, you can use Bash's (Bash! You use a SH, which might not be a Bash) ANSI-C like strings: http://wiki.bash-hackers.org/syntax/quo ... ke_strings


You can also use
Code:
sed -r 's/\t/---/g'


Top
 Profile  
 PostPosted: Tue Apr 27, 2010 4:23 pm   

Joined: Wed Apr 14, 2010 10:23 am
Posts: 17
Which - what I wanted to point out above - depends on his sed and thus he needs to read his documentation. :)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP