Solved

How to parse file in shell script?

Posted on 2008-10-27
4
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 145
format the code in java 6 109
x-bar in Google Sheets 2 93
Problem to App 4 118
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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