Question

Display TAB in Shell Script

Asked by: cutie_smily

Hello All,
I need help in displaying the tab character in UNIX.  I am writing a script which will load the tab delimited text file into a Teradata table. I tried to use the below but it didn’t work

TAB= “\t”
echo "Hello${TAB}World"
TAB1='\t'
echo "Hello${TAB1}WORLD"

Both the echo statements displayed the tab in between the string ‘Hello      World’. But in the script after connecting to the database the $TAB is displayed as \t. Can anyone help me out on this issue to populate the tab character? Below is the line of code I use in my script to populate the TAB character. If I use a tab like “      “ the script works fine.

SET RECORD VARTEXT "$TAB";

$ echo $SHELL
/usr/bin/sh

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-02-23 at 11:23:13ID22409840
Tags

shell

,

tab

,

script

Topics

HP-UX Unix

,

Unix Operating Systems

Participating Experts
5
Points
400
Comments
11

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Check the email by sh shell script
    I would like to have a sh shell script to do the following: I want to check my email for certain time period, say 60 sec. to see I have received any new mail during that period. I also want to have 3 options : 1. An option that allows me to use the prog to check any use...
  2. sh syntax
    I am getting a script (in *unix) to output to file using the following: /usr/local/bpc/bids2login -l <username> -f /etc/bpc.login.pw 2>&1 > <log> & where log is of course the specified name of the log file... Now here's the question.. say I w...
  3. system call and "sh" shell on Solaris
    I'm using Perl 5.6.1. On Solaris platform, I am facing a problem when I invoke shell script using "system" call. Although at the top of the my shell script it is mentioned "#! /bin/ksh", but perl script spawns the command with "sh". Inside the s...
  4. for command in sh
    Hi guyz, I need help in using "for" command in Bourne Shell (sh). When I use "for command" after I "cat" a file which consist of 2 lines ie : testm=`cat $1 | sort` for abc in $testm; do ==> When I call $abc; I should be getting the va...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: KdoPosted on 2007-02-23 at 12:08:04ID: 18598856

Hi cutie,

Where exactly is the tab displaying as '\t'?  Do you know what command or commands are involved?

 

by: TintinPosted on 2007-02-23 at 12:26:24ID: 18598975

You may need to to

/bin/echo "Hello${TAB}World"

or

echo -t "Hello${TAB}World"

 

by: ozoPosted on 2007-02-23 at 14:23:21ID: 18599762

if the \t in your post represents a real tab in your script, then
echo "Hello${TAB}World" should print a real tab

 

by: cutie_smilyPosted on 2007-02-23 at 14:52:52ID: 18599954

Sorry for not being clear. The echo works fine before connecting to the database. Once I connect to the database it is displayed as /t.

Below is the sample script
USERID=$1
DATABASE=$2
TABLE=$3
DATAFILE=$4
TAB="\t"

LOGON=`cat ${BIN_DIR/USERID}_logon`
fastload << EOF
${LOGON}
DROP TABLE ${DATABASE}.${TABLE}_Err1;
DROP TABLE ${DATABASE}.${TABLE}_Err2;
ERRLIMIT 50;

BEGIN LOADING
${DATABASE}.${TABLE}
   ERRORFILES ${DATABASE}.${TABLE}_Err1,
              ${DATABASE}.${TABLE}_Err2
   CHECKPOINT 100000;

SET RECORD VARTEXT "${TAB}";

DEFINE
     Col1       (VARCHAR(5))
     ,col2     (VARCHAR(2))
     ,col3      (VARCHAR(12))

I get an error on the line SET RECORD VARTEXT "${TAB}"; becuase in the log the value of $TAB shows as \t.
Below is the error
0006 SET RECORD VARTEXT "\t";

**** 17:48:25 Incorrect syntax for delimiter character specification
**** 17:48:25 Command not processed

If I hardcode the tab like SET RECORD VARTEXT "    "; then it works but I want to parameterize so that in future I can change to any delimiter I want to.
Thanks

 

by: sjm_eePosted on 2007-02-23 at 14:53:45ID: 18599960

Does your Unix have the 'printf' command? If so, then something like this will do the trick:

printf "%s\t%s\n" Hello World

 

by: KdoPosted on 2007-02-23 at 15:52:14ID: 18600221


Hi cutie,

I found one reference to this issue, and it seems that "\t" works.  But it may require certain conditions that aren't obvious.

  http://www.webservertalk.com/archive286-2005-11-1306144.html
  http://archives.devshed.com/forums/databases-139/loading-tab-delimited-text-file-using-fastload-teradata-1609484.html
 

The links are to the same thread.


Kent

 

by: ozoPosted on 2007-02-26 at 23:19:49ID: 18615233

can you hardcode the tab like
TAB="    "

 

by: siliconbritPosted on 2007-02-28 at 07:25:02ID: 18625715


The problem is that you are passing the value of your delimiter to an input (<<) statement, as part of your database statement. The string passed via (<<) is being sent literally, as you have typed it.

So, in your example, the value of "${TAB}" is expanded literally, so that you are actually passing "\t" as the delimiter, and not a tab character as you expect.

You say you would like to do this in case the delimiter changes in a later iteration, which is commendable when you are writing a generic script, but since your script uses 'drop table', which is an aggressive and job-specific task, I wouldn't bother trying to genericise the script, just type the tab in place.

If you really want a generic script, you should at least:

1) Move the database instructions to an external file, passed to the script either as stdin, or as a named file parameter, or as an editable constant like you have with TAB

2) Instead of varying the value you pass to SET RECORD VARTEXT, you should always use the same delimiter, and instead pass the input file through a sed command to bring it into the format required by the script, by replacing the delimiter for that file with the delimiter enforced by the script.

However, if you insist on doing it this way, pretending to have a generic script, you can achieve this in a number of ways:

A) By breaking down the "<<" statement into three steps:

#step 1
fastload << EOT1
${LOGON}
DROP TABLE ${DATABASE}.${TABLE}_Err1;
DROP TABLE ${DATABASE}.${TABLE}_Err2;
ERRLIMIT 50;

BEGIN LOADING
${DATABASE}.${TABLE}
   ERRORFILES ${DATABASE}.${TABLE}_Err1,
              ${DATABASE}.${TABLE}_Err2
   CHECKPOINT 100000;

SET RECORD VARTEXT
EOT1

# step 2
fastload << $TAB

# step 3
fastload << EOT2
;
DEFINE
     Col1       (VARCHAR(5))
     ,col2     (VARCHAR(2))
     ,col3      (VARCHAR(12))

EOT2


B) By removing the quotes.  Note that the syntax ${..} implies a quote itself, so you are essentially passing ""\t"".  Anyway, just try replacing "${TAB}" with $TAB and see if that works.  If it doesn't, you need to split the << input so that the TAB is passed outside of any other string.

 

by: cutie_smilyPosted on 2007-03-02 at 09:17:20ID: 18642540

Apologize for getting back late.

siliconbrit, Thanks for the detail explanation.
Kdo, Thanks for the search
Ozo, Thanks the TAB="     " worked for me.
Can you please let me know which is the best way to pass a tab in the command line parameter of a script. Ex: in the script TAB=$4 then how can I pass tab to the 4th parameter. Except: "   " is there any better way of doing?

Thanks guys for all the help. I will close the question as soon as i get the answer.

 

by: siliconbritPosted on 2007-03-02 at 10:41:05ID: 18643156


When the shell invokes your command, it will expand any tab character as whitespace, unless that whitespace is quoted.  Therefore, there is no other way of passing your tab as a command-line parameter unless it is quoted.

Even quoted tabs may not pass correctly depending on your prevailing shell and version of UNIX, although in this instance, you should be safe since you are using the core bourne shell.

However, please let me say again that you are approaching this the wrong way around.  You are going to the trouble of creating a 'generic' script, by concerning yourself with the tab character, while the script includes code which is job-specific.

If you genuinely have input files that might be delimited by one of many characters, why dont you make the first job in your script to format the input file into a standard delimited file and *then* pass it to your database job.  So, if the file is delimited by spaces, or commas, replace these with tabs into a temporary file, then work with that file instead.

Furthermore, there is probably a limited number of possible delimiters that could be used depending on your data, and there is probably a known architecture to the input file, so for example it may be possible to work out that the delimiter character is always the sixth character in the file.

 

by: cutie_smilyPosted on 2007-03-02 at 11:28:03ID: 18643501

Thanks siliconbrit for the clear explanation. I made this script to the job specific.

Thanks to all of you for the help.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...