1833187 Members
2465 Online
110051 Solutions
New Discussion

Regarding shell script

 
SOLVED
Go to solution
vind123
Regular Advisor

Regarding shell script

I have a data file of the below format with a lot of records. It has a tab delimiter in it.
I want to extract only the toos_details records which has owner as localexchange and outage as CAD and write it into a new file. How do i do it? Do i need to write a C program or i can achieve through shell script


toos_details
rpt_clli ARTNVACK04T
tfn 22
trfus IT
trfcl PH
ofcls 34
ttm CPR
cllia ARTNVACK04T
dpl 77
clliz SGINTXAA02T
outage CAD
ins Y
oos_ts Sep 22 11:24:10 2003
topas_ts Sep 21 03:50:28 2006
tgak 51303
btfn 0001
num_trk 72
num_toos 72
gap 022
owner localexchange

toos_details
rpt_clli ARTNVACK04T
tfn 8001
trfus AD
trfcl DF
ofcls 35
ttm ACDCG0
cllia ARTNVACK04T
dpl ZZ
clliz TYCRVAAMN01
outage MTC
ins Y
oos_ts Jan 14 15:01:06 2006
topas_ts Sep 21 03:50:28 2006
tgak 8985
btfn 8001
num_trk 1
num_toos 1
gap 070
owner nodal
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor
Solution

Re: Regarding shell script

Hi:

# cat ./extract
#!/usr/bin/perl
local $/ = undef;
@sections = split /toos_details/, <>;
for $_ (@sections) {
print if /owner\s+localexchange/ and /outage\s+CAD/;
}

...run as:

# ./extract file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi (again):

This is better, since it retains the first section header --- the "toos_details":

# cat ./extract
#!/usr/bin/perl
local $/ = undef;
@sections = split( /(?=(toos_details))/, <> );
for $_ (@sections) {
print if /owner\s+localexchange/ and /outage\s+CAD/;
}

...run as:

# ./extract file

Regards!

...JRF...
vind123
Regular Advisor

Re: Regarding shell script

1. I tried the script and i am getting some errors

>cat extract
#!/usr/contrib/bin/perl
local $/ = undef;
sections = split /toos_details/, <>;
sections) {
print if /owner\s+localexchange/ and /outage\s+CAD/;
}

>extract O380391mi
syntax error in file ./extract at line 2, next 2 tokens "local $/ "
syntax error in file ./extract at line 3, next 2 tokens "/,"
Execution of ./extract aborted due to compilation errors.

2. If i redirect the output of the script
do i need to do the below way
extract O380391mi 1>report.out
or
any script needs to be modified?
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi (again):

It appears that you are using a very old versino of Perl. Try this:

#!/usr/contrib/bin/perl
$/ = undef;
@sections = split( /toos_details/, <> );
for $_ (@sections) {
print if /owner\s+localexchange/ && /outage\s+CAD/;
}

Also, do:

# /usr/contrib/bin/perl -v

...If the version isn't 5.x then by all means fetch and install a current version:

http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=PERL

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi (again):

Although I very much prefer Perl, here's an awk solution that fits your data:

# awk 'BEGIN{RS=""};/localexchange/ && /CAD/ {print $0;print ""}' file

Regards!

...JRF...
vind123
Regular Advisor

Re: Regarding shell script

1.

>cat extract1
#!/usr/contrib/bin/perl
$/ = undef;
sections = split( /toos_details/, <> );
sections) {
print if /owner\s+localexchange/ && /outage\s+CAD/;
}

> extract1 O380391mi
Illegal item (LITERAL) as lvalue in file ./extract1 at line 3, next 2 tokens ");"
syntax error in file ./extract1 at line 4, next 2 tokens "sections)"
Execution of ./extract1 aborted due to compilation errors.


2.

> /usr/contrib/bin/perl -v

This is perl, version 4.0

$RCSfile: perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $
Patch level: 36

Copyright (c) 1989, 1990, 1991, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 4.0 source kit.


3. I will try with awk.
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi:

In your last post (above):

1. You mis-pasted what I posted.

2. I suspected that you had a Perl 4. That's ancient! Upgrade to something more useful, as I suggested.

3. In the meantime, the 'awk' solution should help.

Regards!

...JRF...
vind123
Regular Advisor

Re: Regarding shell script

awk script working. thx a lot. Also i want the count of toos_details that has local exchange and CAD. After printing the toos_details is it possible to print the total number of toos_details ? can it be done in the awk script?
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi (again):

Per you last request:

# awk 'BEGIN{N=0;RS=""};/^toos_details/ {N++};/localexchange/ && /CAD/ {print $0;print ""};END{print N}' file

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Regarding shell script

# awk 'BEGIN{RS=""}/CAD/&&/localexchange/{print;l++}END{print l}' infile

~cheers
vind123
Regular Advisor

Re: Regarding shell script

Thanks a lot james and sandy. It's working.
The only change i made to james awk script is
awk 'BEGIN{N=0;RS=""};/localexchange/ && /CAD/ {print $0;print "";N++};END{print N}' file

Also how do i change this one if i want to print all the toos_details other than toos_details which have entries localexchange and cad

Sandman!
Honored Contributor

Re: Regarding shell script

To print all toos_details records...

# awk 'BEGIN{RS=""}/toos_details/' inputfile
vind123
Regular Advisor

Re: Regarding shell script

Sandy,

Basically i want the toos_details records which doesn't have local exchange and CAD to be printed and not all the toos_details.
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi (again):

You wrote, "Basically i want the toos_details records which doesn't have local exchange and CAD to be printed and not all the toos_details".

For this, I suggest:

# awk 'BEGIN{N=0;RS=""};!(/localexchange/ && /CAD/) {print $0;print "";N++};END{print N}' file

...The "!" negates the match expression.

If you are interested in more 'awk' details, I suggest an examination of:

http://www.gnu.org/software/gawk/manual/

Regards!

...JRF...



vind123
Regular Advisor

Re: Regarding shell script

Thanks a lot for the info
vind123
Regular Advisor

Re: Regarding shell script

Thanks a lot