LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: How to read tab character when reading input (https://www.linuxquestions.org/questions/programming-9/bash-how-to-read-tab-character-when-reading-input-508230/)

new_to_bash 12-07-2006 10:33 AM

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)

jlliagre 12-07-2006 11:09 AM

"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)

matthewg42 12-07-2006 11:28 AM

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


new_to_bash 12-07-2006 11:42 AM

thanks a lot for the replies. i'll try these out.

new_to_bash 12-07-2006 01:45 PM

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

jlliagre 12-08-2006 03:18 AM

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.

makyo 12-08-2006 06:15 AM

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


new_to_bash 12-09-2006 07:31 PM

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.


All times are GMT -5. The time now is 07:57 AM.