Solved

How to parse file in shell script?

Posted on 2008-10-27
4
381 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
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 300 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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

12 Experts available now in Live!

Get 1:1 Help Now