I know the way to read line by line:

FILENAME=$1
cat $1 | while read LINE
do
     echo "$LINE"
done

but what if I have something like "Type \t Value" in each line that I need to separate them in LINE.
An example will be something like

Integer 2
String  "abc"
Integer 3

I know many posts talked about awk but I'm wondering if there is a simple way that I can do this within my while loop.

Recommended Answers

All 8 Replies

hi,

cat is useless.

while read line
do echo "$line"
done < "$filename"

your question is not clear: what's the desired output ?

Hi,
Sorry for making it confusing. What I want is to parse these two values (Type and Value) and use them in a command...so my command will look likx xxx$TYPExxx$VALUE, which is why I need to find a way to separate them when I'm parsing my file.
Thanks a lot Watael!

ok, modify read's IFS environment variable:

while IFS=$'\t' read type value
do printf -- 'type: %s -- value: %s\n' "$type" "$value"
done < "$filename"

$'\t' is bash, for sh use litteral tab typing Ctrl-v Ctrl-i

I actually ran into a problem...for some reason everything after $value (i.e., yyyyy) gets cut off but it is totally fine when I use printf or echo when there's nothing after $value. Any thoughs on this issues? Thanks!

FILENAME=$1
while IFS=$'\t' read type value
do
    curl -k "xxxxxtype=$type&value=$valueyyyyy"
done < "$1"

can you provide some sample input?

or is it what you're talking about

filename="$1"
while IFS=$'\t' read type value
do
   echo "$typexxxxx" #doesn't work, because variable $typexxxxx doesn't exist
   echo "${type}xxxxx" #now works: curly braces distinguishes var name from string xxxxx
done < "$filename"

So I have this command

curl -k "xxxxxxtype=$type&value=$valueyyyyy"

that I want to run several times with different types and values. I also have a text file that lists the type and value like

String  s1
String s2
String s3

however when I try to check how the command looks like using echo, the result looks like

curl -k "xxxxxString&s_1

Everything after $value got cut off. The text file is just a simple .txt file wth tab separators.

$ cat yourFile
String        s1
String        s2
String        s3
$ while IFS=$'\t' read type value
> do echo curl -k "xxxxxxtype=${type}&value=${value}yyyyy"
> done < yourFile
curl -k xxxxxxtype=string&value=s1yyyyy
curl -k xxxxxxtype=string&value=s2yyyyy
curl -k xxxxxxtype=string&value=s3yyyyy

Thanks a lot! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.