Find the answer to your Linux question:
Results 1 to 4 of 4
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. ...
  1. #1
    Just Joined!
    Join Date
    Sep 2006
    Posts
    3

    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

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

    Thanks!

  2. #2
    Just Joined!
    Join Date
    Sep 2006
    Posts
    3

    More information

    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 
    
    ----------------------------------------------------------------
    How can I get the output to 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
    Thanks!

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Code:
    while read number name residence; 
      do echo $residence $number $name;
    done < input

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Code:
    awk '
    BEGIN{ print "-----"; OFS="\n"; ORS="\n-----\n"; }
    { print "The number of the person is "$1,
      "The name of the person is "$2,
      $2" lives in "$3 }' data

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...