Solved

first second third words in a line

Posted on 2004-08-17
2
260 Views
Last Modified: 2010-04-21
Hi -

I have a file params.txt which looks like
a   b   c
d   e   f
g   h   i
j    k   l

etc.  , with 3 words per line.

I want to run the programme prog multiple times in a unix loop so that it runs
prog -option1 a -option2 b -option3 c
prog -option1 d -option2 e -option3 f  

etc.

Please tell me an easy way of writing this loop (if there is one).

0
Comment
Question by:glebspy
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 11818743
Hi glebspy,

#!/bin/sh

cat param.txt | while read line
do
           arg1=`echo $line | awk ' { print $1 } ' `
           arg2=`echo $line | awk ' { print $2 } ' `
           arg3=`echo $line | awk ' { print $3 } ' `
           prog -option1 $arg1 -option2 $arg2 -option3 $arg3
done

Sunnycoder
0
 
LVL 20

Expert Comment

by:tfewster
ID: 11822443
Sunnycoder, why do it the hard way? What's wrong with:

while read arg1 arg2 arg3
do
  prog -option1 $arg1 -option2 $arg2 -option3 $arg3
done < params.txt

(Of course, if one of the lines has more than 3 entries, arg3 will be everything up to the end of the line...)
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

820 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