1,105,332 Community Members

Parsing a file with lines separated by tabs

Member Avatar
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Watael
Junior Poster
134 posts since Apr 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 2 [?]
 
0
 

hi,

cat is useless.

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

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

Member Avatar
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
Watael
Junior Poster
134 posts since Apr 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 2 [?]
 
1
 

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

Member Avatar
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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"
Member Avatar
Watael
Junior Poster
134 posts since Apr 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 2 [?]
 
0
 

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"
Member Avatar
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Watael
Junior Poster
134 posts since Apr 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 2 [?]
 
1
 
$ 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
Member Avatar
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a lot! :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article