1826214 Members
3135 Online
109691 Solutions
New Discussion

shell script help

 
SOLVED
Go to solution
Cem Tugrul
Esteemed Contributor

shell script help

Hello Forum,

i have to manipulate many files which rows (cat -vn) always change.But what i have to do;

1 -All these files start (first records start with 811)

2- it is like an array when starts with 811*
then must end with an another record 819*

so this means this is a meaningfull record THEN I have to produce a random file from the original file...Here is an example;

My original_file records like;

811020000060810000056970000100001101213 DE811125281
8120201202504101213000000000000000000001716909789501202504000 0000000171690
8130105077820101213 00 B80037 00000000000000000000000000 000104275
8140381.06225-6007 81062256007 00000000970009900000000017700000000171690 004 0000Y000010 B80037
819030000001000000100000010000001000000100000001716900000000171690000000000000000000000000000000000000000000000
811020000060810000056970000100001101213 DE811125281
8120201202511101213000000000000000000001807509789501202511000 0000000180750
8130105077819101213 00 B80037 00000000000000000000000000 000090000
8140381.15205-5307 81152055307 00000000150009900000000120500000000180750 004 0000Y000010 B80037
819030000001000000100000010000001000000100000001807500000000180750000000000000000000000000000000000000000000000
811020000060810000056970000100001101213 DE811125281
8120201202506101213000000000000000000004044309789501202506000 0000000404430
8130105077815101213 00 B80037 00000000000000000000000000 000178500
8140381.46730-6097 81467306097 00000000170009900000000237900000000404430 004 0000Y000010 B80037
819030000001000000100000010000001000000100000004044300000000404430000000000000000000000000000000000000000000000
811020000060810000056970000100001101213 DE811125281
8120201202505101213000000000000000000000019909789501202505000 0000000001990
8130105077821101213 00 B80037 00000000000000000000000000 000000725
8140382.97485-0825 82974850825 00000000050009900000000003980000000001990 AT 0000Y000010 B80037
819030000001000000100000010000001000000100000000019900000000001990000000000000000000000000000000000000000000000
811020000060810000056970000100001101213 DE811125281
8120201202514101213000000000000000000000024549789501202514000 0000000002454
8130105078047101213 00 B80037 00000000000000000000000000 000000120
8140381.25503-6471 81255036471 00000000060009900000000004090000000002454 004 0000Y000010 B80037
819030000001000000100000010000001000000100000000024540000000002454000000000000000000000000000000000000000000000

SO I have to produced a file witha random filename and this file includes records from my orginal file but it smust be like;

811020000060810000056970000100001101213 DE811125281
8120201202504101213000000000000000000001716909789501202504000 0000000171690
8130105077820101213 00 B80037 00000000000000000000000000 000104275
8140381.06225-6007 81062256007 00000000970009900000000017700000000171690 004 0000Y000010 B80037
81903000000100000010000001000000100000010000000


Any suggestions would be appreciated...
Our greatest duty in this life is to help others. And please, if you can't
13 REPLIES 13
Cem Tugrul
Esteemed Contributor

Re: shell script help

So another file has to be produced again form my orginal file as below with the records; and so on...

811020000060810000056970000100001101213 DE811125281
8120201202506101213000000000000000000004044309789501202506000 0000000404430
8130105077815101213 00 B80037 00000000000000000000000000 000178500
8140381.46730-6097 81467306097 00000000170009900000000237900000000404430 004 0000Y000010 B80037
819030000001000000100000010000001000000100000004044300000000404430000000000000000000000000000000000000000000000
Our greatest duty in this life is to help others. And please, if you can't
James R. Ferguson
Acclaimed Contributor

Re: shell script help

Hi Cem:

If what you want extracted is the groups of lines beginning with "811" and ending with "819" you could do:

# perl -ne '/^811/../^819/ and print' file

If you want only the first group matching this range, do:

# perl -ne '?^811?..?^819? and print' file

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor

Re: shell script help

James,
Thank you...yes i want extracted is the groups of lines beginning with "811" and ending with "819" so this means I will produced may files from my orginal_file BUT

perl -ne '/^811/../^819/ my_orginal_file

not worked!!! Did I do anything wrong?
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: shell script help

I want to produce many files from my orginal_file starts with 811 and ends 819(with end of line 819)
Our greatest duty in this life is to help others. And please, if you can't
James R. Ferguson
Acclaimed Contributor

Re: shell script help

Hi (again):

> yes i want extracted is the groups of lines beginning with "811" and ending with "819" so this means I will produced may files from my orginal_file BUT ...

Since you don't have anything other than successive ranges in your original input you could do:

# perl -ne 'if (?^811?..?^819?) {print};reset,print "\n" if /^819/' file

This would inject a blank line between the ranges. Is that your objective?

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor

Re: shell script help

My original file Ok from your command;
perl -ne 'if (?^811?..?^819?) {print};reset,print "\n" if /^819/' my_orignal_file

BUT this time i need i want to write out these records to new files.That's what i want.

From your command the output is seen;

811020000060810000056970000100001101213 DE811125281
8120201202504101213000000000000000000001716909789501202504000 0000000171690
8130105077820101213 00 B80037 00000000000000000000000000 000104275
8140381.06225-6007 81062256007 00000000970009900000000017700000000171690 004 0000Y000010 B80037
819030000001000000100000010000001000000100000001716900000000171690000000000000000000000000000000000000000000000

811020000060810000056970000100001101213 DE811125281
8120201202511101213000000000000000000001807509789501202511000 0000000180750
8130105077819101213 00 B80037 00000000000000000000000000 000090000
8140381.15205-5307 81152055307 00000000150009900000000120500000000180750 004 0000Y000010 B80037
819030000001000000100000010000001000000100000001807500000000180750000000000000000000000000000000000000000000000

811020000060810000056970000100001101213 DE811125281
8120201202506101213000000000000000000004044309789501202506000 0000000404430
8130105077815101213 00 B80037 00000000000000000000000000 000178500
8140381.46730-6097 81467306097 00000000170009900000000237900000000404430 004 0000Y000010 B80037
819030000001000000100000010000001000000100000004044300000000404430000000000000000000000000000000000000000000000

811020000060810000056970000100001101213 DE811125281
8120201202505101213000000000000000000000019909789501202505000 0000000001990
8130105077821101213 00 B80037 00000000000000000000000000 000000725
8140382.97485-0825 82974850825 00000000050009900000000003980000000001990 AT 0000Y000010 B80037
819030000001000000100000010000001000000100000000019900000000001990000000000000000000000000000000000000000000000

811020000060810000056970000100001101213 DE811125281
8120201202514101213000000000000000000000024549789501202514000 0000000002454
8130105078047101213 00 B80037 00000000000000000000000000 000000120
8140381.25503-6471 81255036471 00000000060009900000000004090000000002454 004 0000Y000010 B80037
819030000001000000100000010000001000000100000000024540000000002454000000000000000000000000000000000000000000000

AND GOES ON...But i need that each [811-819] block has to be >> a new filename
Our greatest duty in this life is to help others. And please, if you can't
James R. Ferguson
Acclaimed Contributor

Re: shell script help

Hi (abain):

> But i need that each [811-819] block has to be >> a new filename

Oh, OK, that's easily done. Try this:

# cat ./rangedivide
#!/usr/bin/perl
use strict;
use warnings;
my ( $fh, $file, $nbr );
$file = "myoutput";
$nbr = 1;
while (<>) {
if (?^811?..?^819?) {
if (/^811/) {
open( $fh, '>', "$file.$nbr" ) or die;
}
print $fh $_;
}
if (/^819/) {
close $fh;
reset;
$nbr++;
}
}
1;

...run as :

# ./rangedivide file

As written, your output file(s) will be named "myoutput.n" where 'n' ranges from one to the number of ranges seen.

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor

Re: shell script help

./rangedivide.sh: 0403-057 Syntax error at line 1 : `(' is not expected.
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: shell script help

oppps...sorry..my fault.
That's what i want.
Thank you very much....
Our greatest duty in this life is to help others. And please, if you can't
James R. Ferguson
Acclaimed Contributor
Solution

Re: shell script help

Hi:

> ./rangedivide.sh: 0403-057 Syntax error at line 1 : `(' is not expected.

Did you cut-and-paste what I posted? Also, don't name the script "*.sh". That infers a shell script as opposed to a Perl one. If you want to use a file extension, use '*.pl'.

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor

Re: shell script help

Thank's so much James...
Our greatest duty in this life is to help others. And please, if you can't
Steven Schweda
Honored Contributor

Re: shell script help

> [...] That infers a shell script [...]

No, it implies a shell script. _You_ might
infer that it's a shell script. (But what's
in the file matters more than the name in
this case.)
James R. Ferguson
Acclaimed Contributor

Re: shell script help

Hi:

@Steven: > [...] That infers a shell script [...]
No, it implies a shell script.

Well, there is the usage notes for the word 'infer' here:

http://dictionary.reference.com/browse/infer

But then this would buttress your suggested distinction :-)

http://www.wsu.edu/~brians/errors/imply.html

Regards!

...JRF...