How to parse file in shell script?

Hi,

I have a file with 3 columns of data. I would like to write a shell script file to parse the data. How to do that?

#!/bin/sh

cat in_file | while read LINE
do
   echo "$LINE"
   # here I got the line from the file

   # now I want to extract data from column1, column2 and column3

   # I tried the following but it doesn't work
   column1=`awk '{print $1}' $LINE`
   # as it complain $LINE is not a file.
 
   # Any idea? What command should I use?
 
  # do something with the data....

done
xewooxAsked:
Who is Participating?
 
omarfaridConnect With a Mentor Commented:
You may do the following:

cat in_file | while read line
do
   set $line
   column1=$1
   column2=$2
   column3=$3
done

using awk is expensive here since it will take more cpu time to get the columns.

You may do

echo $line | awk '{ print $1 }'
0
 
woolmilkporcConnect With a Mentor Commented:
Hi,
try -
while read col1 col2 col3
do
# your code using $col1, $col2, $col3
done < in_file
0
 
woolmilkporcConnect With a Mentor Commented:
Looking at your code above,
cat in_file | while read col1 col2 col3
would work, too.
0
 
xewooxAuthor Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.