Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to parse file in shell script?

Posted on 2008-10-27
4
Medium Priority
?
388 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:xewoox
  • 2
4 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 1200 total points
ID: 22813159
Hi,
try -
while read col1 col2 col3
do
# your code using $col1, $col2, $col3
done < in_file
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 1200 total points
ID: 22813217
Looking at your code above,
cat in_file | while read col1 col2 col3
would work, too.
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 800 total points
ID: 22814110
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
 

Author Comment

by:xewoox
ID: 22871828
Thank you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question