[Solved] Shell script help

Hi fellas,

I have a file that contains text something like this

SNAPSHOT snap-021ede4a vol-bc3f89c0 completed 2012-11-19T06:05:26+0000 100% 170495546006 850 Created by CreateImage(i-6adc0515) for ami-977dfafe from vol-bc3f89c0
TAG snapshot snap-021ede4a project PAC
TAG snapshot snap-021ede4a Name AWSVA-ADPACLD01V
TAG snapshot snap-021ede4a environment DEV
SNAPSHOT snap-02456b66 vol-c54749a8 completed 2012-01-25T11:40:57+0000 100% 170495546006 15 Created by CreateImage(i-3f40a05a) for ami-8967b1e0 from vol-c54749a8
TAG snapshot snap-02456b66 environment DEV
TAG snapshot snap-02456b66 Name ORACLE10204_WITH_DB_CREATED
TAG snapshot snap-02456b66 project Architecture

I have attached only two entries of the contents of the file, it may contain some 50 entries.

I need to write a shell script such that my output will be as shown below,
The snapshot $snap is created by volume :$vol
The $snap should contain the pink colored value and $vol should contain the red colored value.
That is it should print for each entry.

Required output.

1. The snapshot snap-021ede4a is created by volume :vol-bc3f89c0
2. The snapshot snap-02456b66 is created by volume :vol-c54749a8

Thanks,
Kashyap.
[/COLOR][/COLOR][/COLOR]

awk '/SNAPSHOT/{print ++c". The snapshot "$2" is created by volume: "$3;}' filename
2 Likes

Thanks,
It solved my problem.
The code is so simple too.
I am an idiot that i could not writ it.

Your code works fine for me.
But i have a small doubt.

If i want the output to be as shown below,

1. The snapshot snap-021ede4a is created by volume :vol-bc3f89c0
    The details of the volume created are: project PAC , Name AWSVA-ADPACLD01V, environment DEV.

2. The snapshot snap-02456b66 is created by volume :vol-c54749a8
    The details of the volume created are: environment DEV , Name ORACLE10204_WITH_DB_CREATED , project Architecture.

How can i accomplish the task?
I tried a lot but am not able to find a solution to it.:frowning:
Please help me in accomplishing it.

Thanks,
Kashyap.

awk '/SNAPSHOT/ {
 s=$2; v=$3; f++; c++;
} $(NF-1)=="project" {
 p=$NF; f++;
} $(NF-1)=="Name" {
 n=$NF; f++;
} $(NF-1)=="environment" {
 e=$NF; f++;
} f==4 {
 printf "%d. The snapshot %s is created by volume: %s\n", c, s, v;
 printf "The details of the volume created are: project %s, Name %s, environment %s.\n", p, n, e;
 f=0;
} ' filename
1 Like

A Perl solution (not completely finished, week-end starting :)) :

#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = $cur_dir."/file";
my ($record,@fields,$k,$v,%snap,%env,%name,%prj);

open(FILE,"<$filename") or die"open: $!";

while( defined( $record = <FILE> ) ) {
  chomp $record;
  @fields=split(/ /,$record);

  $snap{$fields[1]}=$fields[2] if($fields[0] =~ m/SNAPSHOT/ );
  $env{$fields[2]}=$fields[4] if($fields[3] =~ m/environment/ );
  $name{$fields[2]}=$fields[4] if($fields[3] =~ m/Name/ );
  $prj{$fields[2]}=$fields[4] if($fields[3] =~ m/project/ );
}

while( my ($k,$v) = each(%snap) ) {
  print "$k,$v,$env{$k},$name{$k},$prj{$k}\n";
}

close(FILE);