[Solved] Shell script help | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[Solved] Shell script help

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-15-2013
Kashyap Kashyap is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 15 October 2013, 5:41 AM EDT
Posts: 24
Thanks: 9
Thanked 0 Times in 0 Posts
[Solved] Shell script help

Hi fellas,

I have a file that contains text something like this

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

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




Thanks,
Kashyap.

Last edited by Scrutinizer; 01-15-2013 at 08:55 AM.. Reason: extra code tags
Sponsored Links
    #2  
Old Unix and Linux 01-15-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 24 February 2015, 2:15 PM EST
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

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

The Following 2 Users Say Thank You to Yoda For This Useful Post:
aster007 (01-15-2013), Kashyap (01-16-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-16-2013
Kashyap Kashyap is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 15 October 2013, 5:41 AM EDT
Posts: 24
Thanks: 9
Thanked 0 Times in 0 Posts
Thanks,
It solved my problem.
The code is so simple too.
I am an idiot that i could not writ it.
    #4  
Old Unix and Linux 01-18-2013
Kashyap Kashyap is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 15 October 2013, 5:41 AM EDT
Posts: 24
Thanks: 9
Thanked 0 Times in 0 Posts
Hi bipinathji

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

If i want the output to be as shown below,

Code:
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.Unix or Linux Image
Please help me in accomplishing it.

Thanks,
Kashyap.
Sponsored Links
    #5  
Old Unix and Linux 01-18-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 24 February 2015, 2:15 PM EST
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

Code:
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

The Following User Says Thank You to Yoda For This Useful Post:
Kashyap (01-21-2013)
Sponsored Links
    #6  
Old Unix and Linux 01-18-2013
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 27 February 2015, 1:33 AM EST
Posts: 77
Thanks: 20
Thanked 11 Times in 11 Posts
A Perl solution (not completely finished, week-end starting Unix or Linux Image) :

Code:
#!/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);

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script runs in shell but not cron herot AIX 2 12-31-2012 08:18 PM
Checking missing data's sequence (shell script | UNIX command) septian.tri UNIX for Advanced & Expert Users 2 11-16-2012 12:42 AM
[Solved] Removing control-m characters from shell script abhi_123 UNIX for Dummies Questions & Answers 12 11-12-2012 01:34 AM
run shell script under nohup directly [solved] johninweb Shell Programming and Scripting 0 03-15-2012 02:20 AM
Help to parse csv file with shell script Grhyll UNIX for Dummies Questions & Answers 4 01-19-2012 10:45 AM



All times are GMT -4. The time now is 02:46 PM.