Bash: How to read tab character when reading input
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.