Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-15-2013
Registered User
 
Join Date: Dec 2012
Posts: 16
Thanks: 7
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 07:55 AM.. Reason: extra code tags
Sponsored Links
    #2  
Old 01-15-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,268
Thanks: 150
Thanked 720 Times in 693 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 01-16-2013
Registered User
 
Join Date: Dec 2012
Posts: 16
Thanks: 7
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 01-18-2013
Registered User
 
Join Date: Dec 2012
Posts: 16
Thanks: 7
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.
Please help me in accomplishing it.

Thanks,
Kashyap.
Sponsored Links
    #5  
Old 01-18-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,268
Thanks: 150
Thanked 720 Times in 693 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 01-18-2013
Registered User
 
Join Date: Nov 2012
Posts: 37
Thanks: 11
Thanked 6 Times in 6 Posts
A Perl solution (not completely finished, week-end starting ) :

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
Reply

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
[Solved] Script runs in shell but not cron herot AIX 2 12-31-2012 07:18 PM
[Solved] Checking missing data's sequence (shell script | UNIX command) septian.tri UNIX for Advanced & Expert Users 2 11-15-2012 11:42 PM
[Solved] Removing control-m characters from shell script abhi_123 UNIX for Dummies Questions & Answers 12 11-12-2012 12:34 AM
run shell script under nohup directly [solved] johninweb Shell Programming and Scripting 0 03-15-2012 01:20 AM
[Solved] Help to parse csv file with shell script Grhyll UNIX for Dummies Questions & Answers 4 01-19-2012 09:45 AM



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