LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-05-2008, 02:39 PM   #1
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Rep: Reputation: 0
INPUT, SORT with Alpha numaric characters


Hi all,
I am confused and stucked. Actually i am new in this scripting stuff. I want to give complete txt file as a input to a variable and then sort the data in the file according to the alpha numeric characters, as my text file contains ! and !! and !!! indicating Minor, Major and Critical repectively. Each alpha numeric represent new entry. I am trying to do but all in vain, help needed plz,
codes which i am trying are as follows,


#!/bin/bash
OF=`cat /home/fadain/our\ alkatel\ alarm.txt`
echo $OF

but the out put is not the required one, i was trying to get the contents ( i Mean the acctuall text that file contains as the variable input) and then display it but it aint, second part deals with the sort command i hope, plz help me,
Thanks in Advance
 
Old 02-05-2008, 05:41 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Code:
OF=`cat /home/fadain/our\ alkatel\ alarm.txt`
why are there spaces in the input? should the input to cat be /home/fadain/our/alkatel/alarm.txt?
to sort, is each entry line separated? if so, simply using 'sort filename' should sort it in 'dictionary' order. as always, see man sort for more options and information.

hope it helps

Last edited by nadroj; 02-05-2008 at 05:43 PM.
 
Old 02-05-2008, 07:23 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Please post a sample of your data file, in [c o d e] tags, so we can see how the input is to be manipulated. Is the objective to sort the file line-by-line, ignoring any leading/embedded/trailing '!'s? Is it okay to use a slightly stronger weapon than bash? Say, perl or awk?

--- rod.
 
Old 02-06-2008, 02:12 AM   #4
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Unhappy

Code:
#!/bin/bash
OF=`cat /home/fadain/our\ alkatel\ alarm.txt`
echo $OF
i have no problem with the spaces as my file name contains spaces that why i use back slash for the space. but the codes you told me gives me the same output .
it displays all the files present in the root directory i dont know why ( .

is it okey now.theNbomr.
no brother actually i want to sort my fiel according to the ! sign as this sign in my file represents the entry of every alarm. Each alarm is represented by ! as minor.
In detail
the entry starting with ! sign represents the minor alarm
the entry starting with !! sign represents the major alarm
the entry starting with !!! sign represents the critical alarm

i want to sort them in diffrent file according to 1st the minor entries will display
2nd the major entries will display
3rd the Critical alarm will display,
i need help as i have to submit this to my boss, i am doing internship in Exchange and this is the swtich data that i have to sort. Need urgent help plz.
 
Old 02-06-2008, 02:43 AM   #5
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
Hi there! As what theNbomr said, you need to give your example input in code tags as well as your expected output. This way, your post will get a better attention and hope that someone will come up with a solution to your problem.
 
Old 02-06-2008, 08:08 AM   #6
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Angry

its a long file ,im sending a part of this text file i want to take as input

>> ALAIL;

CMD ALAIL 06095 08-01-30 11-58-23
INP ALAIL 06095 08-01-30 11-58-23
OPTEX=



RES ALAIL 06095 08-01-30 11-58-24
CEN=1/08-01-30/11 H 58 MN 19/STORED ALARMS LIST
PROCESSING TPFFIR ACC


! *A0628/540 /07-12-17/15 H 58/N=7598/TYP=ICT/CAT=SI/EVENT=MAL
/NCEN=MULCT /AM =SMTA1/AGEO=S1-TR01-B03-A085-R000
/TEXAL=AIS/COMPL.INF:
/AF=URMA1
/ ICTRQ AGCA=S1-TR01-B03-A112-R065
/AMET=01-07-02
/AFLR=222-09/CRC=NACT
!!! *A0628/303 /07-12-17/15 H 46/N=7501/TYP=COM/CAT=IM/EVENT=MAL
/NCEN=MULCT /AFUR =URAD- 4/AGEO=BUDLASAN-B01
/TEXAL=SINGLE JUNCT/UT OOS/COMPL.INF:
/CN=3

as i told befor ! & !!! showing minor & critical alarms.its a log file of alkatel switch & i have to extract
!!!
date & time
event
& texal
i tried so many times but whenever i try to take file as input by code


[c o d e]
#!/bin/bash
OF=`cat /home/fadain/our\ alkatel\ alarm.txt`
echo $OF

i got the output

>> : command not found

if i removed these operators from the start of file i again got the same output

ALAIL ; :command not found

even this file doesnt have executeable permission but still i am getting this message
i am so confused as i am still at my first step
please help me, why it doesnt take the whole file as text input rather the first word and ignorin the rest of file
i am waiting for your reply soon
i am quiet stucked a input
plz help

Last edited by nabmufti; 02-06-2008 at 09:22 AM.
 
Old 02-06-2008, 10:11 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
So you want each of the entries marked by the '!', '!!' & '!!!' symbols copied to their own files?
This should come close. I am unable to discern time components of each alarm record, only dates.
Code:
#! /usr/bin/perl -w
#
#   LQnabmufti.pl
#
#   Usage: LQnabmufti.pl <alarmlog.txt>
#
#
#==========================================================

use strict;

my $date;
my $event;
my $texal;
my $alarmType;

my $noalarm  = 0;
my $critical = 1;
my $major    = 2;
my $minor    = 3;
my @alarmName = ('no alarm', 'critical', 'major', 'minor');

my %alarm;

    $alarmType = $noalarm;
    # print $alarmName[ $alarmType ],"\n";
    
    while(<>){
    
        #
        #   Match and save alarm elements of interest
        #
        if( $_ =~ m/\/([0-9][0-9]-[0-9][0-9]-[0-9][0-9])\// ){
            $date = $1;
        }

        if( $_ =~ m/EVENT=([^\/\n]+)/ ){
            $event = $1;
        }

        if( $_ =~ m/TEXAL=([^\/\n]+)/ ){
            $texal = $1;
        }
            

        #
        #   If new alarm record found, save old alarm if any, and start new record
        #
        if( $_ =~ m/^!!!/ ){
        
            if( $alarmType != $noalarm ){
                # print "$alarmName[$alarmType]: '$date' '$event' '$texal'\n";
                push @{ $alarm{$alarmName[$alarmType]} }, [$date, $event, $texal];
            }

            $alarmType = $critical;                        
        }
        elsif( $_ =~ m/^!!/ ){
        
            if( $alarmType != $noalarm ){
                # print "$alarmName[$alarmType]: '$date' '$event' '$texal'\n";
                push @{ $alarm{$alarmName[$alarmType]} }, [$date, $event, $texal];
            }

            $alarmType = $major;                        
        
        }
        elsif( $_ =~ m/^!/ ){
        
            if( $alarmType != $noalarm ){
                # print "$alarmName[$alarmType]: '$date' '$event' '$texal'\n";
                push @{ $alarm{$alarmName[$alarmType]} }, [$date, $event, $texal];
            }

            $alarmType = $minor;                        
        }
    }

    #
    #   At EOF, we may have a record hanging. Save it.
    #
    if( $alarmType != $noalarm ){
        # print "$alarmName[$alarmType]: '$date' '$event' '$texal'\n";
        push @{ $alarm{$alarmName[$alarmType]} }, [$date, $event, $texal];
    }


    #
    #   Iterate over all alarm types, and save to files, name by alarm type
    #
    foreach my $alarmName ( keys %alarm ){
        # print $alarmName,"\n";

        open( LOG, ">LQnabmufti.$alarmName" ) || die "Cannot open log file for write : $!\n";
        #
        #   Iterate over all records in each alarm type. 
        #   Write comma separated fields, records deleimited by newlines.
        #
        foreach my $alarmInst ( @{$alarm{$alarmName}} ){
            print LOG $alarmInst->[0], ", ", $alarmInst->[1], ", ",$alarmInst->[2], "\n"; 
        }
        close LOG;
    }
Make the code executable, and run it with the input filename as the only argument. This is moderately challenging, due to the limited structure of the input data. There are ways to make the code less redundant; I didn't want to make it any less readable.
--- rod.

Last edited by theNbomr; 02-06-2008 at 10:13 AM.
 
Old 02-06-2008, 01:23 PM   #8
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Original Poster
Rep: Reputation: 0
this is well and good and i reallye appreciate your quick response.
but i want this programme in bash ,
one thing i asker is that how can take the input of a file & then i trim that data to my desired string
i used the code

[c o d e]
#!/bin/bash
alarmfile=/root/alakatel.txt
while read line
{
do echo $line
}done < $alarmfile
[/c o d e]
 
Old 02-06-2008, 01:32 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by nabmufti View Post
this is well and good and i reallye appreciate your quick response.
but i want this programme in bash ,
Good luck with that. It would have been nice to specify that at the outset.
How about this? :
Code:
#! /bin/bash
perl LQnabmufti.pl $1
--- rod.

Last edited by theNbomr; 02-06-2008 at 01:34 PM.
 
Old 02-06-2008, 02:36 PM   #10
nabmufti
LQ Newbie
 
Registered: Feb 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Unhappy

let me explain one more time please,MAIN PROBLEM I have a text file which i have mentioned, its basically a log file in which diffrent types of alarm entries are thrown into the file by Alkatel switch software, I want to extract all three types of alarm and sort every alarm entry in another file. Below is the given part of the file containing all three alram types which are distinguished by ! Minor !! Major !!! Critical Respectively.
Code:
>> ALAIL;

   CMD ALAIL  06095   08-01-30 11-58-23
   INP ALAIL  06095   08-01-30 11-58-23
       OPTEX=



   RES ALAIL  06095   08-01-30 11-58-24
    CEN=1/08-01-30/11 H 58 MN 19/STORED ALARMS LIST
    PROCESSING TPFFIR ACC


!   *A0628/540 /07-12-17/15 H 58/N=7598/TYP=ICT/CAT=SI/EVENT=MAL
    /NCEN=MULCT /AM   =SMTA1/AGEO=S1-TR01-B03-A085-R000
    /TEXAL=AIS/COMPL.INF:
    /AF=URMA1
    /  ICTRQ  AGCA=S1-TR01-B03-A112-R065
    /AMET=01-07-02
    /AFLR=222-09/CRC=NACT
!!! *A0628/303 /07-12-17/15 H 46/N=7501/TYP=COM/CAT=IM/EVENT=MAL
    /NCEN=MULCT /AFUR =URAD-  4/AGEO=BUDLASAN-B01
    /TEXAL=SINGLE JUNCT/UT OOS/COMPL.INF:
    /CN=3


!!! *A0628/306 /08-01-23/07 H 20/N=9619/TYP=COM/CAT=IM/EVENT=MAL
    /NCEN=MULCT /AFCN =CNLM3-4/AGEO=MKRASHID-B02-A003
    /TEXAL=CN ISOLATED
!!! *A0628/303 /07-12-17/15 H 46/N=7503/TYP=COM/CAT=IM/EVENT=MAL
    /NCEN=MULCT /AFUR =URAD- 12/AGEO=KHOTYWAL-B01
    /TEXAL=SINGLE JUNCT/UT OOS/COMPL.INF:
    /CN=0
!!! *A0628/089 /08-01-29/22 H 14/N=1760/TYP=ENP/CAT=IM/EVENT=MAL
    /NCEN=MULCT /AFUR =URAD- 11/AGEO=QADIRPUR-B01
    /TEXAL=GENERATING UNIT


!!  *A0628/320 /08-01-17/13 H 47/N=8062/TYP=COM/CAT=ID/EVENT=MAL
    /NCEN=MULCT /AFUR =URAL- 15/AGEO=S1-TR01-B04
    /TEXAL=FAULTY UNIT/COMPL.INF:
*   #F0612/T11F14/NCEN=MULCT /08-01-30/11 H 33/NAM=ODCAB   /TDA=0001
    /N=7064/NIV=2/ENS=006/SENS=013-000-000/P='0228'H/CN=02/PAR='03'H
    /EM: AFUR =URAL- 15
What i have to EXTRACT

Code:
!
      date & time
   07-12-17/15 H 58/

     EVENT=MAL

    /NCEN=MULCT /AM   =SMTA1/AGEO=S1-TR01-B03-A085-R000

    /TEXAL=AIS/COMPL.INF
I know that now I am bit annoying you but actually I am in need of help please. Again THanx
 
Old 02-06-2008, 05:14 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by nabmufti View Post
let me explain one more time please,MAIN PROBLEM I have a text file which i have mentioned, its basically a log file in which diffrent types of alarm entries are thrown into the file by Alkatel switch software, I want to extract all three types of alarm and sort every alarm entry in another file. Below is the given part of the file containing all three alram types which are distinguished by ! Minor !! Major !!! Critical Respectively.
What i have to EXTRACT

Code:
!
      date & time
   07-12-17/15 H 58/

     EVENT=MAL

    /NCEN=MULCT /AM   =SMTA1/AGEO=S1-TR01-B03-A085-R000

    /TEXAL=AIS/COMPL.INF
I know that now I am bit annoying you but actually I am in need of help please. Again THanx
Okay, this is more helpful. I now think I see what you expect to interpret as 'time'. What I do not understand is why the line
Code:
/NCEN=MULCT /AM   =SMTA1/AGEO=S1-TR01-B03-A085-R000
gets output, and why the lines starting at
Code:
/AF=URMA1
doeas not. Other than that, the code I produced pretty much meets your spec. Did you actually try it? You should be able to modify the regular expression to match the time-of-day portion. If you can explain how to identify the above mentioned component, I can suggest a regex that will match it, too. Then, all that is left is to translate the code to bash. This will be very difficult, however, since really, bash is the wrong tool for this job, at least as you described it. You might be able to cobble something together with bits of sed and/or awk, but that isn't really any different from using perl, which many would argue is the perfect tool for the job. While it may be possible to accomplish your task using strictly bash, it will be a lot more work, won't be as readable, or maintainable, probably a lot slower (and I know log files can become large in many cases). A language with a strong regular expression capability is definitely called for here, and languages such as perl, sed & awk (maybe python, I'm not sure) fall in that family. Bash does not. Having said that, if you do accomplish your goal using bash, I would love to see the code. Please post it here.
--- rod.

Last edited by theNbomr; 02-06-2008 at 05:16 PM.
 
Old 02-06-2008, 05:43 PM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Here is a version that matches the time-of-day (I think), and is a bit more concise overall.
Code:
#! /usr/bin/perl -w
#
#   LQnabmufti2.pl
#
#   Usage: LQnabmufti2.pl <alarmlog.txt>
#==========================================================

use strict;

my $date;
my $event;
my $texal;
my $alarmType;

my $noalarm  = 0;
my $critical = 3;
my $major    = 2;
my $minor    = 1;
my @alarmName = ('no alarm', 'minor', 'major', 'critical');

my %alarm;

    $alarmType = $noalarm;
    
    while(<>){
        #
        #   Match and save alarm elements of interest
        #
        if( $_ =~ m/\/([0-9][0-9]-[0-9][0-9]-[0-9][0-9]\/[0-9]+\s+H\s+[0-9]+\/)/ ){
            $date = $1;
        }

        if( $_ =~ m/EVENT=([^\/\n]+)/ ){
            $event = $1;
        }

        if( $_ =~ m/TEXAL=([^\/\n]+)/ ){
            $texal = $1;
        }
        #
        #   If new alarm record found, save old alarm if any, and start new record
        #
        if( $_ =~ m/^(!+)/ ){

            if( $alarmType != $noalarm ){
                push @{ $alarm{$alarmName[$alarmType]} }, [$date, $event, $texal];
            }

            #   We get the type of alarm from the length of the string of '!'s
            #
            $alarmType = length($1);                        
        }
    }
    #
    #   At EOF, we may have a record hanging. Save it.
    #
    if( $alarmType != $noalarm ){
        push @{ $alarm{$alarmName[$alarmType]} }, [$date, $event, $texal];
    }
    #
    #   Iterate over all alarm types, and save to files, name by alarm type
    #
    foreach my $alarmName ( keys %alarm ){

        open( LOG, ">LQnabmufti.$alarmName" ) || die "Cannot open log file for write : $!\n";
        print LOG "Date/time            Event     Texal\n";
        #
        #   Iterate over all records in each alarm type. 
        #   Write comma separated fields, records deleimited by newlines.
        #
        foreach my $alarmInst ( @{$alarm{$alarmName}} ){
            print LOG "'", $alarmInst->[0], "','", $alarmInst->[1], "','",$alarmInst->[2], "'\n"; 
        }
        close LOG;
    }
--- rod.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
INPUT, SORT with Alpha numaric characters nabmufti Linux - Newbie 1 02-05-2008 03:57 PM
Regular expression to match 4 or more alpha characters sixerjman Programming 15 11-27-2006 12:03 AM
non alpha characters frankwc Linux - Security 1 06-24-2004 05:40 PM
Can only input certain characters? mooresd3 Linux - General 2 02-15-2004 12:37 AM
How To Input Characters In A List bprasanth_20 Programming 10 10-30-2003 12:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration