Operating System - HP-UX
1753753 Members
5190 Online
108799 Solutions
New Discussion юеВ

Pattern Search Fixed width Position

 
lnair
Occasional Contributor

Pattern Search Fixed width Position

Record length is 820 Characters fixed width position with 2 million rows ///


112223333sugar458696963
125632233water4589sugar
125632233people63336666

Output would be something like this


Output:

File 1
-------
112223333sugar4586969633

File 2
-------
125632233water45869sugar
125632233people633366666
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: Pattern Search Fixed width Position

If you use the command line it will take TWO passes through the data:

# grep "^\([0-9]\{9\}\)sugar\(.*$\)" testfile1 > outfile1
# grep -v -f outfile1 testfile1
125632233water4589sugar
125632233people63336666
# grep -v -f outfile1 testfile1 > outfile2
# cat outfile1
112223333sugar458696963
# cat outfile2
125632233water4589sugar
125632233people63336666
# cat testfile1
112223333sugar458696963
125632233water4589sugar
125632233people63336666
# grep "^\([0-9]\{9\}\)sugar\(.*$\)" testfile1 > outfile1
# grep -v -f outfile1 testfile1 > outfile2
# cat outfile1
112223333sugar458696963
# cat outfile2
125632233water4589sugar
125632233people63336666
#

Because it requires TWO passes and a LOT of OVERHEAD, I suggest you write a perl script to do it.


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Pattern Search Fixed width Position

#!/usr/bin/perl
#
open(ORIGfileptr,"< ORIGFILE");
open(OUToneFileptr,"> file1");
open(OUTtwoFileptr,"> file2");
while () {
if ( m/^([0-9]{9})sugar(.*$)/ ) {
printf OUToneFileptr $_;
} else {
printf OUTtwoFileptr $_;
}
}
close(ORIGfileptr);
close(OUToneFileptr);
close(OUTtwoFileptr);


change ORIGFILE (on the open line (the one in quotes)) to the real file name.


live free or die
harry
Live Free or Die
Jeroen Peereboom
Honored Contributor

Re: Pattern Search Fixed width Position

I do not understand your question. Maybe you can clarify a little bit.

* What is the criterion to put a line in file 1 or file 2? The first number (9 digits)?
* Only 2 output files?

JP.
lnair
Occasional Contributor

Re: Pattern Search Fixed width Position

Thanks for the Suggestion;

However I was looking for an slightly different answer; to be precise, Pattern "Sugar" could be anything (non empty characters);

So basically the comparison would be something like this For instance(from position 10-14 non-empty characters spooled to 1 file and rest empty characters spooled to 2 file )


112223333sugar458696963
125632233water4589sugar
125632233people63336666
harry d brown jr
Honored Contributor

Re: Pattern Search Fixed width Position

#!/usr/bin/perl
#
open(ORIGfileptr,"< ORIGFILE");
open(OUToneFileptr,"> file1");
open(OUTtwoFileptr,"> file2");
while () {
if ( m/^([0-9]{9})([A-Za-z]{5})(.*$)/ ) {
printf OUToneFileptr $_;
} else {
printf OUTtwoFileptr $_;
}
}
close(ORIGfileptr);
close(OUToneFileptr);
close(OUTtwoFileptr);

The change says that any ALPHA string five characters long in positions 10-14, go to "file1", all others go to "file2".

TO SHOW SPACES in your examples, use the "+" sign.

live free or die
harry
Live Free or Die