Solved

How to parse file in shell script?

Posted on 2008-10-27
4
380 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 300 total points
Comment Utility
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 300 total points
Comment Utility
Looking at your code above,
cat in_file | while read col1 col2 col3
would work, too.
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 200 total points
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now