Create a two column output in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a two column output in bash
# 1  
Old 06-30-2014
Wrench Create a two column output in bash

Hi!
I m newbie for scripting. My requirement is to create a host file as below from the output of aws api.

Code:
Hostname PrivateIP
abc x.y.x.z
cde a.b.c.c

and so on.

I have the following script,
Code:
#!/bin/bash
export AWS_ACCESS_KEY=abc
export AWS_SECRET_KEY=cde
export EC2_URL=https://url.com
source ~/.bashrc
ec2-describe-instances --filter "group-name=mygroup" > /tmp/info
name=$(cat /tmp/info| grep 'Hostname' | cut -f 5)
ip=$(cat /tmp/info| grep 'INSTANCE' | cut -f 18)
echo -e $name"\t"$ip

currenltly the output is in one line.
I m sure i need a for loop. But not sure how to do it.
Please advice.

Last edited by Scrutinizer; 06-30-2014 at 12:23 PM.. Reason: CODE tags
# 2  
Old 06-30-2014
You echo just one line (which may have multiple names and ips). Please post the output of ec2-describe-instances.
# 3  
Old 06-30-2014
Code:
RESERVATION     r-a64da0e5      322802824935    mygroup
INSTANCE        i-ca4bef89      ami-896c96fe    ec2-52-72-117-279.eu-west-1.compute.amazonaws.com       ip-10-42-41-91.eu-west-1.compute.internal       running mykey       0               t1.micro  2014-06-19T10:54:01+0000 eu-west-1b      aki-52a34525                    monitoring-disabled     52.72.117.279   10.42.41.91                    ebs                                     paravirtual     xenUPcZb1403098656835      sg-05241d61     default false
BLOCKDEVICE     /dev/sda1       vol-7e2e2772    2014-06-18T13:37:40.000Z        true
TAG     instance        i-ca4bef89      Name    linux-1
TAG     instance        i-ca4bef89      Hostname        linux-1
RESERVATION     r-a64da0e5      322802824935    mygroup
INSTANCE        i-ca4bef89      ami-896c96fe     ec2-51-72-117-279.eu-west-1.compute.amazonaws.com        ip-10-41-41-91.eu-west-1.compute.internal       running mykey       0                t1.micro  2014-06-19T10:54:01+0000 eu-west-1b       aki-52a34525                    monitoring-disabled     51.72.117.279    10.41.41.91                    ebs                                      paravirtual     xenUPcZb1403098656835      sg-05241d61     default false
BLOCKDEVICE     /dev/sda1       vol-7e2e2772    2014-06-18T13:37:40.000Z        true
TAG     instance        i-ca4bef89      Name    linux-2
TAG     instance        i-ca4bef89      Hostname        linux-2


Last edited by cuteboyucsc; 06-30-2014 at 01:45 PM..
# 4  
Old 06-30-2014
There's no "Hostname" string in that sample.
# 5  
Old 06-30-2014
Sorry. that was incorrect. I have edited it now. The output is as above.
# 6  
Old 06-30-2014
Not sure what
Code:
export AWS_ACCESS_KEY=abc
export AWS_SECRET_KEY=cde

are for and where they are used. Not sure either, why you are referencing field 18 ("default") in your script. I had to use 15 (or 14) to get an IP number.
Try
Code:
printf "\t%s\t%s\n" Hostname privateIP  
while read IN X X HO TMPNM X X X X X X X X X TMPIP REST
        do [ "$IN" = "INSTANCE" ] && IP=$TMPIP
           [ "$HO" = "Hostname" ] && { NM=$TMPNM;  printf "\t%s\t%s\n" $NM $IP; }
        done <  /tmp/info

or pipe the command output immediately to that while loop...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create the space only in the Third column by awk

Hi All , I am having an input file like this Input file r s e y Pin Numbers s n eppppppppppppppppppc ... (3 Replies)
Discussion started by: kshitij
3 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Create file with name first column name.

Hi please help me in resolving the scenario. Source File: col1 col3 ----- ----- file1 data1 file2 data2 file1 data3 file3 data4 file5 data5 .. ... .... and so on. Result should be:4 files created. first file, file1.txt (no need to worry about .txt extension) will... (1 Reply)
Discussion started by: maks475
1 Replies

4. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

5. Programming

How to create this on bash?

I'm trying to create a script with the ff array array 1 - foo@fee.com,fee@fee.com,fyii@fee.com (1 line) array 2 - paul what I want is to have the ff output in a text file foo@fee.com: paul fee@fee.com: paul fyii@fee.com: paul Could this be done on a bash script? I'm trying this... (1 Reply)
Discussion started by: packets
1 Replies

6. Shell Programming and Scripting

create new column for filename

Hi, I created a list with 2 columns. Each line is from a different file. I am getting these with a loop in Perl. I would like to add a 3rd column with the name of the file that the line is coming from. I usually use pr to print the filename but this is not working here ... I was wondering if... (5 Replies)
Discussion started by: danieladna
5 Replies

7. Shell Programming and Scripting

Bash: create a report with grep output?

Greetings. I need to generate a simple report via Bash (Korn?) with this raw data Test_Version=V2.5.2 Test_Version=V2.6.3 Test_Version=V2.4.7 Test_Version=V2.5.2 Test_Version=V2.5.2 Test_Version=V2.5.1 Test_Version=V2.5.0 Test_Version=V2.3.9 ... (3 Replies)
Discussion started by: alan
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