LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-27-2009, 05:15 AM   #1
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 438

Rep: Reputation: 30
Shell regex - add new field


I have a text file which is divided into a parts called "case".
Each case contain fields beginning with sign $ and @.
Here is asample:
Code:
                case "6": ### - rcmProcStaCP2DownEvent
                        $bs2sisTrapSender = $1
                        $bs2sisTrapTime = $2
                        @Summary = "description_1"
                        @Severity = 5
                        @Type = 1
                        @ExpireTime = 3600*12

                case "7": ### - rcmIncompatibilityAlarm
                        $bs2sisTrapSender = $1
                        $bs2sisTrapTime = $2
                        $bs2sisTrapAlarmFaultId = $3
                        $bs2sisTrapCause = $4
                        $bs2sisTrapAlarmX733Cause = $5
                        $bs2sisTrapInformation = $6
                        @Summary = "Description_2"
                        @Severity = 5
                        @Type = 1
                        @ExpireTime = 3600*12
I'd like to add at the end of each case new field @Identifier.
@Identifier should contain 4 repeatable fields + all fields beginning with $ in each case.
Here is an example:
Code:
                case "6": ### - rcmProcStaCP2DownEvent
                        $bs2sisTrapSender = $1
                        $bs2sisTrapTime = $2
                        @Summary = "description_1"
                        @Severity = 5
                        @Type = 1
                        @ExpireTime = 3600*12

                        @Identifier = @Node + " " + @AlertGroup + " " + @AlertKey + " " + @Type + " " +$bs2sisTrapSender+ " " +$bs2sisTrapTim

                case "7": ### - rcmIncompatibilityAlarm
                        $bs2sisTrapSender = $1
                        $bs2sisTrapTime = $2
                        $bs2sisTrapAlarmFaultId = $3
                        $bs2sisTrapCause = $4
                        $bs2sisTrapAlarmX733Cause = $5
                        $bs2sisTrapInformation = $6
                        @Summary = "Description_2"
                        @Severity = 5
                        @Type = 1
                        @ExpireTime = 3600*12
                        
                        @Identifier = @Node + " " + @AlertGroup + " " + @AlertKey + " " + @Type + " " +$bs2sisTrapSender+ " " +$bs2sisTrapTim+ " " +$bs2sisTrapAlarmFaultId+ " " +$bs2sisTrapCause+ " " +$bs2sisTrapAlarmX733Cause+ " " +$bs2sisTrapInformation
Can you guys help me out how to do that ?
 
Old 02-27-2009, 05:44 AM   #2
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
Do you really want a blank line preceding it? If so, precede the second line with `\n'.

Code:
sed  '/case "/,/^$/{ /^$/i\
                        @Identifier = @Node + " " + @AlertGroup + " " + @AlertKey + " " + @Type + " " +$bs2sisTrapSender+ " " +$bs2sisTrapTim+ " " +$bs2sisTrapAlarmFaultId+ " " +$bs2sisTrapCause+ " " 
+$bs2sisTrapAlarmX733Cause+ " " +$bs2sisTrapInformation
}' infile
If the last record doesn't end with a blank line, you will need to add a rule to cover it as well.

Last edited by jschiwal; 02-27-2009 at 05:48 AM.
 
Old 02-27-2009, 05:56 AM   #3
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 438

Original Poster
Rep: Reputation: 30
I dont think this will work as I expect...
Please, note that amount of $ fields is diffrent in each case.

Last edited by czezz; 02-27-2009 at 06:05 AM.
 
Old 02-28-2009, 03:13 PM   #4
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
My perl offering...
Code:
#! /usr/bin/perl -w
use strict;

{ local $/ = "";
    my @cases = <>;
    foreach my $case ( @cases ){
        print $case;
        print "\n",'                        @Identifier = @Node + " " + @AlertGroup + " " + @AlertKey + " " + @Type ';
        my @dollarBss = $case =~ m/\s*(\$bs2sis.+)\s+=\s+/g;
        foreach my $dollarBs ( @dollarBss ){
            print " + \" \" + $dollarBs";
        }
        print "\n\n";
    }    
}
Run with input file as commandline argument.
--- rod.

Last edited by theNbomr; 02-28-2009 at 03:23 PM. Reason: Bugs fixed
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can i add my own field(of 2 bytes) in the ip header structure raklo Linux - Networking 5 08-18-2011 02:17 PM
replaced variable field within shell script using sed hamimi Linux - Newbie 2 05-28-2008 08:01 AM
performing regex on shell script variable? ocicat Programming 3 07-17-2007 02:10 PM
Perl add numbers in 2nd field twantrd Programming 6 10-19-2006 08:26 PM
How to extract the 16 byte source name field (Name to add) from the NetBIOS fram (NBF Bassam Linux - Networking 0 05-23-2004 04:58 AM


All times are GMT -5. The time now is 03:55 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration