Sponsored Content
Full Discussion: Loop column output
Special Forums UNIX Desktop Questions & Answers Loop column output Post 302253824 by handband2 on Sunday 2nd of November 2008 09:27:45 PM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
TALK(1) 						    BSD General Commands Manual 						   TALK(1)

NAME
talk -- talk to another user SYNOPSIS
talk person [ttyname] DESCRIPTION
talk is a visual communication program which copies lines from your terminal to that of another user. Options available: person If you wish to talk to someone on your own machine, then person is just the person's login name. If you wish to talk to a user on another host, then person is of the form 'user@host'. ttyname If you wish to talk to a user who is logged in more than once, the ttyname argument may be used to indicate the appropriate terminal name, where ttyname is of the form 'ttyXX'. When first called, talk sends the message Message from TalkDaemon@his_machine... talk: connection requested by your_name@your_machine. talk: respond with: talk your_name@your_machine to the user you wish to talk to. At this point, the recipient of the message should reply by typing talk your_name@your_machine It doesn't matter from which machine the recipient replies, as long as his login-name is the same. Once communication is established, the two parties may type simultaneously, with their output appearing in separate windows. Typing control-L '^L' will cause the screen to be reprinted, while your erase, kill, and word kill characters will behave normally. To exit, just type your interrupt character; talk then moves the cursor to the bottom of the screen and restores the terminal to its previous state. Permission to talk may be denied or granted by use of the mesg(1) command. At the outset talking is allowed. Certain commands, in particu- lar nroff(1) and pr(1), disallow messages in order to prevent messy output. ENVIRONMENT
If the TALKHOST environment variable is set, its value is used as the hostname the talk packets appear to be originating from. This is use- ful if you wish to talk to someone on another machine and your internal hostname does not resolve to the address of your external interface as seen from the other machine. FILES
/etc/hosts to find the recipient's machine /var/run/utmp to find the recipient's tty SEE ALSO
mail(1), mesg(1), who(1), write(1) HISTORY
The talk command appeared in 4.2BSD. BUGS
The version of talk released with 4.3BSD uses a protocol that is incompatible with the protocol used in the version released with 4.2BSD. BSD
January 7, 2007 BSD
All times are GMT -4. The time now is 08:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy