LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-07-2006, 11:33 AM   #1
new_to_bash
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Rep: Reputation: 0
Bash: How to read tab character when reading input


Hello,
i'm new to bash and searched the forum before asking this question. I'm reading input from a file line by line, however i need to check if the first string of a line is preceded by a tab. i'm using following code to read input line by line, but this ignores any whitespace/tab at the begining and end of the line. can anyone plz help me?

line=""

while read line
do parseLine $line;
done < <(cat $1)
 
Old 12-07-2006, 12:09 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,517

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
"read" is already somewhat parsing the line in words. Tab being by default a separator is not making it to the line variable.

You can remove tab from the separators with using IFS=" ", or you can really read a better way the whole line with the "line" command:

while line=$(line)
 
Old 12-07-2006, 12:28 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You could use head to read a line.
Code:
line=$(head -n 1)
To verify that it includes tabs, you can pass it through od:
Code:
echo "$line" | od -tc
Which might output something like this:
Code:
0000000   h   e   l   l   o  \t   w   o   r   l   d  \n
0000014
 
Old 12-07-2006, 12:42 PM   #4
new_to_bash
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
thanks a lot for the replies. i'll try these out.
 
Old 12-07-2006, 02:45 PM   #5
new_to_bash
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlliagre
You can remove tab from the separators with using IFS=" ",
thanks a lot. this worked. sorry, but this will be my last question on this topic. now that i'm able to read characters how can can compare if the input read has tab?
i tried
if [ $1 = "\t" ]
it didn't work
 
Old 12-08-2006, 04:18 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,517

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
A portable way would be:
Code:
if [ "$(expr $1 : '\(.\)'" = "<TAB>" ]
With bash, this should works too:
Code:
if [ "${1:0:1}" = "<TAB>" ]
Replace <TAB> with the tab character in the previous samples.
 
Old 12-08-2006, 07:15 AM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

Here is a script that illustrates the setting of TAB into a variable, the use of IFS to allow reading of TABS, and the test for a single TAB character ... cheers, makyo
Code:
#!/bin/sh

# @(#) s1       Demonstrate setting TAB into variable.

# Set tab character into variable, and verify it is there.
# Alternate method might be:
# mytab="       "
# where character between " " is typed (not copy/paste) as tab.
#
# Highlight characters by surrounding with innocuous ":".

mytab=`echo -e "\t"`
echo "The character :$mytab: is a tab." |
cat -tv

# Show how to use IFS for testing.  Loop forever until tab.
#
# Set Internal Field Separator to space only.

oldifs="$IFS"
IFS=" "

# Issue a prompt.

echo
echo " Enter data, only a single tab will cause end (or control-c)"

i=1
while :
do
        read line
        if [ "$line" = "$mytab" ]
        then
                echo " You entered a single TAB on iteration $i, done."
                exit 0
        else
                echo " Iteration $i, you entered :$line:, not a tab alone, continuing."
        fi
        i=`expr $i + 1`
done

IFS="$oldifs"

# Other work as necessary.

exit 0
 
Old 12-09-2006, 08:31 PM   #8
new_to_bash
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
thanks thanks thanks a lot to jlliagre and makyo. you guys really healped me. it worked!!! you guys are great. its tough being a newbie and help from ppl like you gets us going. thanks again.
 
  


Reply

Tags
bash, file, from, read, tab


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you change how many spaces contains a TAB character in the console (tty) ? zahadumy Linux - Software 2 05-18-2006 03:09 AM
reading one character spank Programming 4 12-24-2005 07:17 AM
Kmix no input tab BiZ123 Linux - Software 1 12-01-2005 10:22 PM
verifing input from bash read michael_util Programming 1 01-13-2005 10:09 AM
reading gdesklets/rss-grabs in new tab in firefox sterrenkijker Linux - Software 0 09-22-2004 02:29 PM


All times are GMT -5. The time now is 05:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration