Start New Discussion Reply to this Discussion Parsing a file with lines separated by tabs
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.
Related Article: File Renaming & Folder Sorting
is a Shell Scripting discussion thread by John Linux that has 1 reply, was last updated 1 year ago and has been tagged with the keywords: file, rename, bash, mac, osx.
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
hi,
cat is useless.
while read line
do echo "$line"
done < "$filename"
your question is not clear: what's the desired output ?
Watael
Junior Poster
121 posts since Apr 2012
Reputation Points: 4
Solved Threads: 26
Skill Endorsements: 2
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!
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
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
Watael
Junior Poster
121 posts since Apr 2012
Reputation Points: 4
Solved Threads: 26
Skill Endorsements: 2
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"
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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"
Watael
Junior Poster
121 posts since Apr 2012
Reputation Points: 4
Solved Threads: 26
Skill Endorsements: 2
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.
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
$ 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
Watael
Junior Poster
121 posts since Apr 2012
Reputation Points: 4
Solved Threads: 26
Skill Endorsements: 2
malluce
Newbie Poster
6 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.0853 seconds
using 2.68MB