Error in the code in Linux | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Error in the code in Linux

UNIX for Dummies Questions & Answers


Tags
awk, bash, linux, shell scipting

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-31-2014
Palak Sharma Palak Sharma is offline
Registered User
 
Join Date: Mar 2014
Last Activity: 23 July 2014, 7:32 AM EDT
Posts: 58
Thanks: 16
Thanked 0 Times in 0 Posts
Error Error in the code in Linux

I am writing a shell script. I am using the
Code:
amazon-cli

tools to fetch the data from the Amazon console.

My requirement is to write few attributes of the instances in a file in a specific order like Instance-id , name, owner, cost-centre in a row for a single instance.

ec2-describe-instances output

Code:
RESERVATION r8  10927   
    INSTANCE    i-c42  ami-452c        ip-.internal    stopped risdhat 0       t1.micro    2013-12-19T09:43:07+0000    us-east-1d          windows monitoring-disabled     10.250.168.225  vpc-86e4    subnet-0fa7b    ebs                 hvm xen Ccurn1387446186661  sg-a9cb default false   
    BLOCKDEVICE /dev/sda1   vol-28b48d5f    2013-12-19T09:43:09.000Z    true        
    NIC eni-26f3e052    subnet-0fa7b    vpc-860e4   1527    in-use  10.250.168.225      true
    NICATTACHMENT   eni-attach-ea22a386 0   attached    2013-12-19T09:43:07+0000    true
    GROUP   sgcb    Test
    PRIVATEIPADDRESS    10.250.168.225      
    TAG instance    i-c42   Description Machine for SAML implementation
    TAG instance    i-c42    Name    Rii_Win_SAML
    TAG instance    i-c42   Owner   Rii Pandey
    TAG instance    i-c42   cost.centre   1243



RESERVATION r-48465168  109030037527    
INSTANCE    i-55         ami-b7f6c5de       ip-10-250-171-248.ec2.internal  running Jyoti_Test_Virginia 0   [marketplace: aacglxeowvn5hy8sznltowyqe]    t1.micro    2014-03-19T09:16:56+0000    us-east-1c  aki-88aa75e1            monitoring-disabled     10.250.171.248  vpc-86bab0e4    subnet-3cf58314 ebs                 paravirtual xen VYeFw1395220615808  sg-a9b2a9cb default false   
BLOCKDEVICE /dev/sda    vol-37ff097b    2014-03-19T09:17:00.000Z    false       
NIC eni-3619f31d    subnet-3cf58314 vpc-86bab0e4    109030037527    in-use  10.250.171.248      true
NICATTACHMENT   eni-attach-9210dee8 0   attached    2014-03-19T09:16:56+0000    true
GROUP   sg-a9b2a9cb Test
PRIVATEIPADDRESS    10.250.171.248      
TAG instance    i-55    Description     Server for testing RDS feature in us-east-1c AZ
TAG instance    i-55    name    RDS_Machine (us-east-1c)
TAG instance    i-55    owner   Joseph


Note:

The ec2-describe-instances command produces the details of all the AWS servers in the Cloud.I have given details of only two instances.

I am using this code:




Code:
ec2-describe-instances | awk '/(Name|Owner|cost\.centre)/&&/TAG/{if (!($3 in c)) {c[$3];b[++i]=$3;} a[$3,$4]=$5 " " $6} END {print "Instance id", "Name", "Owner", "Cost.centre"; for(k=1; k<=i; k++) print b[i], a[b[i],"Name"], a[b[i],"Owner"], a[b[i],"cost.centre"]}' OFS='\t' | column -ts$'\t'

Output After running the code :


Code:
Instance id            Name                      Owner         Cost.centre
i-55              RDS_Machine (us-east-1c)         Joseph             
i-55              RDS_Machine (us-east-1c)         Joseph             
i-55             RDS_Machine (us-east-1c)          Joseph

My code is fetching the detail of only a single instance repeatedly.

Desired output:


Code:
Instance id          Name                         Owner         Cost.centre
i-c42             Rii_Win_SAML                     Rii Pandey     1234
 i-55              RDS_Machine (us-east-1c)    Joseph

I am struggling a lot on this.

Any lead is highly appreciated.


Thanks
Sponsored Links
    #2  
Old 03-31-2014
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 25 July 2014, 12:44 PM EDT
Location: Aachen, Germany
Posts: 3,886
Thanks: 62
Thanked 922 Times in 875 Posts
At first glance, the loop variable is k in your END for loop, and you reference i as the index to the arrays, which will be always the index to the last record encountered.
Sponsored Links
    #3  
Old 03-31-2014
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 26 June 2014, 10:29 AM EDT
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts

Code:
ec2-describe-instances output | awk '/INSTANCE/{i=$2}$0~/[Nn]ame|[Oo]wner/{sub(".*"$5,$5);i=i OFS $0}/cost.centre/{sub(".*"$5,$5);i=i OFS $0;print i;i=z}END{print i}' OFS=:

    #4  
Old 03-31-2014
Palak Sharma Palak Sharma is offline
Registered User
 
Join Date: Mar 2014
Last Activity: 23 July 2014, 7:32 AM EDT
Posts: 58
Thanks: 16
Thanked 0 Times in 0 Posts
@ctsgnb: Hi Sir,

I am getting an error :


Code:
ec2-describe-instances output | awk '/INSTANCE/{i=$2}$0~/[Nn]ame|[Oo]wner/{sub(".*"$5,$5);i=i OFS $0}/cost.centre/{sub(".*"$5,$5);i=i OFS $0;print i;i=z}END{print i}' OFS=:

Client.InvalidInstanceID.Malformed: Invalid id: "output"

---------- Post updated at 10:33 PM ---------- Previous update was at 09:12 PM ----------

Also I can not see the entire code. Kindly split it into multiple lines.

I cant read it till the end
Sponsored Links
    #5  
Old 04-01-2014
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 July 2014, 1:05 AM EDT
Location: San Jose, CA, USA
Posts: 4,149
Thanks: 162
Thanked 1,414 Times in 1,199 Posts
This discussion seems to be the same issue as in another thread you started here: How to resolve this error?

This thread is closed. Continue any further discussion the thread reference above.
The Following User Says Thank You to Don Cragun For This Useful Post:
Palak Sharma (04-01-2014)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Linux xmllint schema validation fails but error code 0 mknag Shell Programming and Scripting 4 07-03-2013 07:38 AM
Linux through Java code Dorothy Linux 1 08-19-2012 01:18 PM
error code 137 and error code 35072 laxmi131 Shell Programming and Scripting 1 06-18-2010 12:46 AM
Linux source code pritish.sas Shell Programming and Scripting 1 07-21-2009 04:49 AM
source code for linux naren_chella Linux 2 03-06-2006 11:56 PM



All times are GMT -4. The time now is 03:06 AM.