Extract batch based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract batch based on condition
# 1  
Old 03-21-2015
Extract batch based on condition

HI,
I have a file as mentioned below. Here one batch is for one user id.Batch starts from |T row and ends at .T row. I want to create a new file by reading this file. The condition is for record 10(position 1-2), if position 3 to position 17 is 0 then delete the entire batch and write into the new file.Here i want to delete entire batch for 12345678 and 4589722 because 10 record is 0 for position 3 to 17. The new file will have only last batch.

Code:
|T 20150315
01456200080043000
02453980000000000
03682902950092200
05342901230000000
07980341985040000
09103530000000000
10000000000000000
.T 12345678
|T 20150315
01000000000000000
02000000000000000
03000000000000000
07000000000000000
09000000000000000
10000000000000000
.T 4589722
|T 20150315
01345893000000000
05000000000000000
09000000000000000
10000034578690980
.T 8726345


Last edited by Don Cragun; 03-21-2015 at 07:35 AM.. Reason: Add CODE tags.
# 2  
Old 03-21-2015
I don't understand what you're trying to do here.

You say that if your "10" record contains enough zeros, you want to delete the entire batch and write into the new file. And, you say that the new file will contain only the last batch.

You don't show any desired sample output, and the requirements above are in direct conflict with each other.
# 3  
Old 03-21-2015
Hi Don,
The new will have only below entries.
|T 20150315 01345893000000000 05000000000000000 09000000000000000 10000034578690980 .T 8726345

This is because you can see the row "10" have records > 0 from position 3 to 17. Position 3 to 17 is transaction amount. I want to create a new file which will have only those batches whose transaction amount is >0.

Last edited by abhi.mit32; 03-21-2015 at 08:51 AM.. Reason: some more information was required.
# 4  
Old 03-21-2015
Quote:
Originally Posted by abhi.mit32
...
The new will have only below entries.
|T 20150315 01345893000000000 05000000000000000 09000000000000000 10000034578690980 .T 8726345

This is because you can see the row "10" have records > 0 from position 3 to 17. Position 3 to 17 is transaction amount. I want to create a new file which will have only those batches whose transaction amount is >0.
If Perl is an option, then here's a program that reads the file line-by-line, stores each line of a record in an array and then either skips the record or prints it as per the rule.

Code:
$
$ cat -n f27
     1  |T 20150315
     2  01456200080043000
     3  02453980000000000
     4  03682902950092200
     5  05342901230000000
     6  07980341985040000
     7  09103530000000000
     8  10000000000000000
     9  .T 12345678
    10  |T 20150315
    11  01000000000000000
    12  02000000000000000
    13  03000000000000000
    14  07000000000000000
    15  09000000000000000
    16  10000000000000000
    17  .T 4589722
    18  |T 20150315
    19  01345893000000000
    20  05000000000000000
    21  09000000000000000
    22  10000034578690980
    23  .T 8726345
$
$
$ perl -lne 'if (/^\|T/) {
                 $in = 1;
                 $skip = 0;
                 push @x, $_;
             } elsif ($in and /^10000000000000000/) {
                 $skip = 1;
                 @x = ();
             } elsif ($skip) {
                 next;
             } elsif ($in and not $skip and !/^\.T/) {
                 push @x, $_;
             } elsif (not $skip and /^\.T/) {
                 push @x, $_;
                 foreach $i (@x) {print $i}
                 @x = ();
                 $in = 0;
                 $skip = 0;
             }
            ' f27
|T 20150315
01345893000000000
05000000000000000
09000000000000000
10000034578690980
.T 8726345
$
$

Alternatively, here's a Perl program that reads an entire record (that starts at the "|T" line and ends at ".T" line) as a chunk and then either skips it or prints it as per the rule.

Code:
$
$ perl -lne 'BEGIN {undef $/}
             while(/^(\|T.*?\.T \d+)/msg) {
                 $rec = $1;
                 next if $rec =~ m/10000000000000000/;
                 print $rec;
             }
            ' f27
|T 20150315
01345893000000000
05000000000000000
09000000000000000
10000034578690980
.T 8726345
$
$

# 5  
Old 03-21-2015
Is this possible to write in a shell script or any unix tool?
# 6  
Old 03-21-2015
Quote:
Originally Posted by abhi.mit32
Is this possible to write in a shell script or any unix tool?
Yes, I'm sure it is possible.
# 7  
Old 03-21-2015
Quote:
Originally Posted by abhi.mit32
Is this possible to write in a shell script or any unix tool?
Sure - give it a try.
Any particular reason you don't consider perl being a "unix tool"?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

extract file .jar from package and batch

Thank You to vbe For This Useful Post i want to extract file. jar from package and batch ? (1 Reply)
Discussion started by: moh_abaloo
1 Replies

2. Windows & DOS: Issues & Discussions

Batch file loop and increment value for condition

I am trying to have the below batch file do following two things: 1. only allow the values YES,yes,Y,y, or NO,no,N,n 2. increment the counter %var1 only if answer to question 2 is "y" and not able to get the syntax correct. If %var1%=1 then I am trying to display function :end. Thank you :).... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Copy down based on condition

Hello: I need to copy down some data from the previous record in to the next record based on the below conditions If position 41- 59 of the current record is same as the previous record and the value of position 62 is not equal to 1 then copy the previous records value for positions... (1 Reply)
Discussion started by: techedipro
1 Replies

4. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

5. Shell Programming and Scripting

Help with extract info if fulfill condition required

Input file (4 DATA record shown in this case): DATA AA0110 ACCESSION AA0110 VERSION AA0110 GI:157412239 FEATURES Location/Qualifiers length 1..1170 1..1700 /length="1170" position ... (5 Replies)
Discussion started by: perl_beginner
5 Replies

6. Shell Programming and Scripting

ksh: how to extract strings from each line based on a condition

Hi , I'm a newbie.Never worked on Unix before. I want a shell script to perform the following: I want to extract strings from each line ,based on the type of line(Nameline,Subline) and output it to another file.Below is a sample format. 2010-12-21 14:00"1"Nameline"Midterm"First Name:Jane ... (4 Replies)
Discussion started by: angie1234
4 Replies

7. Shell Programming and Scripting

Script to batch pdfjoin based on pdfgrep output

I have a situation in which I'm given a bunch of pdf files which are all single pages with employee ID's on an independent line. I need to collate all of the pages by employee ID. Piecemeal, I can find a particular employee ID by just using pdfgrep. I could also do something like this: find .... (3 Replies)
Discussion started by: nopposan
3 Replies

8. Shell Programming and Scripting

extract xml tag based on condition

Hi All, I have a large xml file of invoices. The file looks like below: <INVOICES> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>1234</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>2345</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME>... (9 Replies)
Discussion started by: angshuman
9 Replies

9. Shell Programming and Scripting

Extract value inside <text> tag for a particular condition.

Hi All! I have obtained following output from a tool "pdftohtml" :: So, my input is as under: <text top="246" left="160" width="84" height="16" font="3">Business purpose</text> <text top="260" left="506" width="220" height="16" font="3">giving the right information and new insights... (3 Replies)
Discussion started by: parshant_bvcoe
3 Replies
Login or Register to Ask a Question