Loop column output


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Loop column output
# 1  
Old 11-02-2008
Loop column output

I need help in what to do with a bash script? I'm trying to run a command to output the data from a table and then insert it into commands. Looping for each row of data.

For example the output data from a table:

Code:
10 John house
20 Jane apt
30 Joe townhome

Then I need to take the output from the data and insert it into another command, so for example my output would look like:

Code:
-----
The number of the person is 10
The name of the person is John
John lives in a house
-----
The number of the person is 20
The name of the person is Jane
Jane lives in a apt
-----
The number of the person is 30
The name of the person is Joe
Joe lives in a townhome

The code I have is:
Code:
#!/bin/bash

echo
echo "-----------------------------------------------------------------"

DATA=`cat data.txt`

for i in $DATA; do

    NUM=$(echo $i |awk '{print $1}');
    NAME=$(echo $i |awk '{print $2}');
    LOC=$(echo $i |awk '{print $3}');    

    echo "The number of the person is $NUM"
    echo "The name of the person is $NAME"
    echo "$NAME lives in a $LOC"

echo
echo "-----------------------------------------------------------------"
echo
done

The output is:
Code:
-----------------------------------------------------------------

The number of the person is 10
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is John
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is house
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is 20
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is Jane
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is apt
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is 30
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is Joe
The name of the person is 
 lives in a 

-----------------------------------------------------------------


The number of the person is townhome
The name of the person is 
 lives in a 

----------------------------------------------------------------

Can anyone help or point me where to go on how to do this?

Thanks!
# 2  
Old 11-03-2008
You can work on something like this,

Code:
while read line
 do 
   set -- $line
   # you can also use echo -e 
   printf -- "-----\nThe number of the person is $1\nThe name of the person is $2\n$2 lives in a $3\n"
 done < file

Output

Code:
-----
The number of the person is 10
The name of the person is John
John lives in a house
-----
The number of the person is 20
The name of the person is Jane
Jane lives in a apt
-----
The number of the person is 30
The name of the person is Joe
Joe lives in a townhome

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

2. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

3. Shell Programming and Scripting

Download images from the first column and rename it with the second column in a loop: Please help!

Dear Friends, I have a very little knowledge on shell scripting. I am stuck with a problem for which I need an expert advice. I have a .txt file "image_urls.txt" which contains the data like this imageurl ... (2 Replies)
Discussion started by: Praveen Pandit
2 Replies

4. Shell Programming and Scripting

adding one more column in a loop

data1 1 0.01 3 5 1 0.6 2 1 data2 2 0.02 3 5 2 0.3 2 1 data3 3 0.01 3 5 3 0.01 2 1 output 1 0.01 data1 2 0.02 data2 3 0.01 data3 3 0.01 data3 I want to print 1st, 2nd column and the filename when second column is less than 5. (3 Replies)
Discussion started by: johnkim0806
3 Replies

5. Shell Programming and Scripting

while loop output

:wall:Hi I am a beginner to unix In a shell script i see the below code # set admin email so that you can get email ADMIN=someone@somewhere.com host=`hostname` date=`date` # set alert level 70% is default ALERT=70 df -h | grep / | grep -v '^Filesystem|tmpfs|cdrom' | awk '{ print... (1 Reply)
Discussion started by: prabhu_kumar
1 Replies

6. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

7. Shell Programming and Scripting

grep/awk on a single column in a for loop

Hi I'm trying to loop through a small list of id's and then pull out a few columns if the id matches that found in column 2 of the larger file. I managed to get one command to work awk -F " " '{if ($2 == '154080196') print $2,$3,$4}' tst.txt | less However, when I try it in a for loop I... (3 Replies)
Discussion started by: flotsam
3 Replies

8. Shell Programming and Scripting

Converting line output to column based output

Hi Guys, I am trying to convert a file which has a row based output to a column based output. My original file looks like this: 1 2 3 4 5 6 1 2 3 1 2 3 (8 Replies)
Discussion started by: npatwardhan
8 Replies

9. Shell Programming and Scripting

top output for six processes with the same name, output changed from column to row

Hi, I have a system under test, and I use a script that does a ps. The output, is in the following format, it's basically the timestamp, followed by the rss and vsize. 09:03:57 68404 183656 68312 181944 69860 217360 67536 182564 69072 183172 69032 199276 09:04:27 68752 183292 70000 189020... (5 Replies)
Discussion started by: Bloke
5 Replies

10. Shell Programming and Scripting

4 column tsv file, output 1 specific column

Hello all siteexplorer.search.yahoo.com can output results in tsv format, when opened in excel I get 4 columns. I would like to wget that file, which I can do. I would then like to pull the 2nd column and output it only. I've searched around and found a few bits and pieces but nothing I've... (6 Replies)
Discussion started by: casphar
6 Replies
Login or Register to Ask a Question