Selective printing based on matched record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selective printing based on matched record
# 15  
Old 04-10-2017
Quote:
Originally Posted by MadeInGermany
The index() function is described in
Code:
man awk

The (c && c--) is an implicit if condition; if it evaluates to true it prints $0 (the default action for an implicit if).
If c is non-zero it evaluates c-- that is also non-zero but then decrements c. After c times c is zero and it does no further decrement and no default-print.
I don't like c-->0 because it keeps c decrementing, hoping it will not underflow or wrap around after a zillion times.
Code:
awk -v A=5 '(/^0[67][MV]/ && index($0,"003529")==26) {c=A+1} (c && c--)' file

Note that index($0,"003529")==26 may give a false negative result if the string 003529 appears starting at offset 26 and at some other place in the line before position 26. If there is any chance that that string could occur at an earlier position in the line, the ERE used by Scrutinizer (updated to match your complete requirements ^0[67][MV].{22}003529) or the substring check used by RudiC (substr($0, 26, 6) == "003529") would be safer.
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 04-10-2017
thank you all for the commands given to me in the earlier posts.

Iv got a couple of other records that I wanna fetch and it is related to the same file so not wanting to open a new thread. Would have pasted this earlier but this requirement never came into the picture.

sample input
Code:
06M5279484366528007 0328 003529         J27300022      MICROSOFT   *MSN
06<1      000000001344392   JPN151-85830 MSBILL.INFO      F
06<A17087454000328651551    MSBILL.INFO  JPN0000000000000000000000000060300
06<BZ20C0WS0L208                              5627
06=0000000000000000001344MCCAW5PIO0328      4816    0
06=51709000100020 09000--
06V4789071000648915 0328 003529R         28600656      MICROSOFT   *XBOXLIVE
06<1      000000001343124ON CANL5N 8L900 MSBILL.INFO     M 2005650000
06<A17087354000328251263    MSBILL.INFO  ON*0000000000000000000000000000000
06<BZ20G0W6Q6PNX                              1928
06#1524V1928 00000000134312400307087366122975VCXS581601    P
06#31Z20G0W6Q6PNX              Z000000       000000001343
70 311200213883             000000007416         13287229
06V4980208991901179 0327 003529         727400138      MICROSOFT   *STORE
06<1      000000013824392   JPN151-85830 MSBILL.INFO     M 2005650000
06<A17086754000327691767    MSBILL.INFO  JPN0000000000000000000000000000000
06<BZ20E0VRXY54Q                              5627
06#1524V5627 00000001382439200467087101247989TLSK481601    G
06#31Z20E0VRXY54Q   1205422440 U000000       000001382400
[70 311160631884             000000001550         69285782

I now want to fetch the related 70 records line from the file. basically, each of the record/s, has a corresponding 70 record line which can be found after a collection of such 06/07 records. For eg, I have highlighted the 70 record line for the first collection of related 06/07 records. This collection of 06/07 records can be 1 or more than 1, but the related 70 records will be directly after these records and will be only 1 in count.

thanks again

Last edited by dsid; 04-10-2017 at 10:42 AM.. Reason: putting in more description
# 17  
Old 04-10-2017
Quote:
Originally Posted by dsid
can you please explain the code in RED



I tweaked your code a bit
Code:
awk -v A=5 '/^0[67][MV].{22}003529/{c=A+1}c-->0'

and it is running fine in AIX, but why is it not running on Linux. Both OS has GNU version of awk
Older GNU awk versions need the option --re-interval
Code:
awk --re-interval -v A=5 '/^0[67][MV].{22}003529/{c=A+1}c-->0' file


Last edited by Scrutinizer; 04-10-2017 at 03:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

awk Selective printing of fields

Hi Gurus, I have following input file. I tried multiple awk combinations to print selected columns without success. HEX ID Name ver FLRGT Start Time Total Shared End Date ----- -------- --- ------ ------------------------ -------------- -------... (4 Replies)
Discussion started by: shunya
4 Replies

3. Shell Programming and Scripting

Selective printing

I have the following contents in a file ---- CRITICAL: altered for /usr/bin/bin1 ---- OK: /usr/sbin/bin2 result fine ---- OK: /usr/sbin/bin3 result fine ---- CRITICAL: altered for /usr/bin/bin4 ---- OK: /usr/bin/bin5 result fine ---- OK: /usr/bin/bin6 result fine ---- CRITICAL: altered for... (9 Replies)
Discussion started by: anil510
9 Replies

4. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

5. Shell Programming and Scripting

selective printing

hi all from below text "abcd,SYS_12345,xyz,PQR, ," I want to print only "abcd,SYS,xyz,PQR, ," i.e. taking only first three 3 chars from 2 string of comma separated file thanks (4 Replies)
Discussion started by: JoeColeEPL9
4 Replies

6. Shell Programming and Scripting

Perl: selective printing of lines

Hi, I have a file with lines like this. 2 7 18 ggcgt anna 2 7 18 hhchc sam 3 7 18 hhdjcc ross 4 7 18 hhcjd jenny 0 8 21 jjdhs sam 3 8 21 kkok bush 2 9 24 kosss BrenhamIf the values of the second column are equal, print only those lines with the least first column value. So in... (5 Replies)
Discussion started by: polsum
5 Replies

7. Shell Programming and Scripting

Awk selective printing

Hi, i need help to print number from different field INPUT: Student1 10 20 Student2 30 40 Student3 50 60 Student4 70 80 Desired Output: 1 20-30 2 40-50 3 60-70 Thank you! (5 Replies)
Discussion started by: saint2006
5 Replies

8. Shell Programming and Scripting

sed selective printing

Hi, I have an xml file having serveral smiliar lines as below <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" PHYSICALLENGTH ="15"... (3 Replies)
Discussion started by: dips_ag
3 Replies

9. Shell Programming and Scripting

selective printing of lines

Hi all , i need to grep for a string in a text file and print the string and the 3rd line above it. As always , Thanks. (4 Replies)
Discussion started by: okiedokie
4 Replies

10. Shell Programming and Scripting

selective printing awk

Hi there my file looks like this 1 a b c d e f 2 a b b c d e f f g h e t t 3 a c b d e f 4 a b c i want to print the line which has the fields containing ONLY a b c, in this case the line 4. How can i awk it !!!? Many Thanks in advance! (8 Replies)
Discussion started by: saint2006
8 Replies
Login or Register to Ask a Question