Selective printing based on matched record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selective printing based on matched record
# 8  
Old 04-07-2017
If you're happy with that sed solution, why didn't the grep given satisfy your needs?
Try
Code:
awk '/^0[67][MV]/ && substr($0, 26, 6) == "003529" {L = NR + 6} NR < L' file

# 9  
Old 04-07-2017
Quote:
Originally Posted by RudiC
If you're happy with that sed solution, why didn't the grep given satisfy your needs?
Try
Code:
awk '/^0[67][MV]/ && substr($0, 26, 6) == "003529" {L = NR + 6} NR < L' file

using Linux on deaktop but grep on AIX production doesn't support the -A switch
# 10  
Old 04-08-2017
Hi.
Quote:
Originally Posted by dsid
... grep on AIX production doesn't support the -A switch
Here are some alternatives for versions of grep that do not support options -A and -B:
Code:
Print lines above, below pattern-matched line (context, window); "only" string matching pattern

        1) GNU grep -A -B; /usr/sfw/bin/ggrep on some Solaris

        2) peg, a perl script, does not use "-o" like GNU grep;
           allows -A, -B
           http://www.cpan.org/authors/id/A/AD/ADAVIES/peg-3.10 (or later)

        3) ack, a perl script, can use "-o" like GNU grep; allows -A, -B
           https://github.com/petdance/ack2 (2016.11)
           https://metacpan.org/pod/distribution/ack/ack

        4) cgrep does not use "-o" like GNU grep; allows -nline,
        +nline for context like GNU grep -A, -B; matching can cross lines
        http://sourceforge.net/projects/cgrep/

        5) bool, prints context in bytes; matching can cross lines

        6) xtcgrep, extension of CPAN grep; allow -C context; allows -o;
           complicated calling sequence; source:
           http://cpansearch.perl.org/src/MNEYLON/File-Grep-0.01/Grep.pm
           ( also, but stale: http://freecode.com/projects/greppm )

        7) sift (aka "gogrep"), extremely fast grep work-alike, allows -A, -B, -C
           can do "-o" using "--replace", matching can cross lines
           https://sift-tool.org/ (verified 2015.11.05)

        8) ag, "The Silver Searcher", similar to, but faster than ack.
           In Debian 8.4 repository, as "silversearcher-ag".

        9) pt, "The Platinum Searcher", similar to ag, faster than ack.
           https://github.com/monochromegane/the_platinum_searcher
           Written in "go". (verified 2016.04.25)

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 11  
Old 04-08-2017
Code:
awk -v A=5 '/^06M.{22}003529/{c=A+1}c-->0' file



--
Quote:
Originally Posted by RudiC
If you're happy with that sed solution, why didn't the grep given satisfy your needs?
[..]
The grep -A option is a GNU and BSD extension

Last edited by Scrutinizer; 04-08-2017 at 03:13 PM..
# 12  
Old 04-08-2017
Some awk do not have {n,m} in their ERE, but all have index():
Code:
awk '(/^0[67][MV]/ && index($0,"003529")==26) {c=6} (c && c--)' file

This User Gave Thanks to MadeInGermany For This Post:
# 13  
Old 04-10-2017
Quote:
Originally Posted by MadeInGermany
Some awk do not have {n,m} in their ERE, but all have index():
Code:
awk '(/^0[67][MV]/ && index($0,"003529")==26) {c=6} (c && c--)' file

can you please explain the code in RED

Quote:
Originally Posted by Scrutinizer
Code:
awk -v A=5 '/^06M.{22}003529/{c=A+1}c-->0' file
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

Last edited by dsid; 04-10-2017 at 07:36 AM.. Reason: running fine on AIX
# 14  
Old 04-10-2017
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

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