Solved

first second third words in a line

Posted on 2004-08-17
2
259 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

770 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