|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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
|
||||
|
||||
|
Code:
awk '/SNAPSHOT/{print ++c". The snapshot "$2" is created by volume: "$3;}' filename |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Thanks,
It solved my problem. The code is so simple too. I am an idiot that i could not writ it. |
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|