ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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,
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
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.
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?
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.
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.
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
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.
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]
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.
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.
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.