The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > UNIX Desktop for Dummies Questions & Answers
.
google unix.com



UNIX Desktop for Dummies Questions & Answers Discuss UNIX and Linux user interfaces like GNOME, KDE, CDE, and Open Office here. All UNIX and Linux Newbies Welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 06:27 AM
Compare 2 files for a single column and output differences samit_9999 UNIX for Dummies Questions & Answers 1 04-23-2008 01:02 PM
Can we use 'tr' command to print 5th column of output of 'ls -l' Nidhi2177 Shell Programming and Scripting 4 09-17-2007 07:53 AM
Sub. numbers in column of output with If ja156194 Shell Programming and Scripting 11 02-12-2007 01:52 PM
flags to suppress column output, # of rows selected in db2 sql in UNIX jerardfjay Shell Programming and Scripting 1 11-02-2005 06:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-02-2008
handband2 handband2 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 5
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 (permalink)  
Old 11-03-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0