Selective printing based on matched record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selective printing based on matched record
# 1  
Old 04-07-2017
Selective printing based on matched record

Hi,

I have file with thousands of lines somewhat similar to the below 6 lines

Code:
06MXXXXXXXXXXXXXXXX 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--

I want to search for the line starting with a '06 then an M or V' Or' 07 then an M or V' with any characters till 003529 (which is at the 26-31st position). Once this match is found, it should print out the next 5 lines, including the matched line, so in effect a total of 6 lines.

I was using a somewhat similar awk command to search for lines which have 003529 at the specific position
, but couldn't figure out how to write the regex. Also couldn't figure out how I would print the next 5 (total 6 including the match) lines.
Code:
awk '{if (substr($0,26,6)=="003529")print $0}' file

what I had thought was if I could get the matched line $0 as NR==1, then I could print out NR1 to NR==6 when there was a match.

Please need some expert advice on this
# 2  
Old 04-07-2017
Hello dsid,

Could you please try following and let me know if this helps.
Code:
awk '/^06[MV].*003529/'  Input_file

NOTE: You had mentioned like after chars M or V, 1 more time M or V should come but couldn't see this into your output, let us know if this is having more conditions etc.

Thanks,
R. Singh
# 3  
Old 04-07-2017
Try also
Code:
grep -A5 "^0[67][MV].\{22\}003529" file

OR

Code:
awk '/^0[67][MV]......................003529/ {L = NR + 6} NR < L' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-07-2017
Quote:
Originally Posted by RavinderSingh13
Hello dsid,

Could you please try following and let me know if this helps.
Code:
awk '/^06[MV].*003529/'  Input_file

NOTE: You had mentioned like after chars M or V, 1 more time M or V should come but couldn't see this into your output, let us know if this is having more conditions etc.

Thanks,
R. Singh

I had tried your command with a little tweak

Code:
awk '/^0[67][MV].*003529/'

and the output I got was what it was supposed to give. However, it isn't exactly suitable for me as I am looking for only those lines which have 003529 in the 26-31 position.

Also there is either a 06M or 06V or 07M or 07V in the line that I need to search. When the regex matches, it should print the next 5 lines, including $0, in total 6 lines.

Hope I am clear now.

---------- Post updated at 10:28 AM ---------- Previous update was at 10:19 AM ----------

Quote:
Originally Posted by RudiC
Try also
Code:
grep -A5 "^0[67][MV].\{22\}003529" file

OR

Code:
awk '/^0[67][MV]......................003529/ {L = NR + 6} NR < L' file

Hi RudiC,

Can you please explain to me what does the part in RED mean?

Also is there a way where we can specify that 003529 comes between position 26,31. Reason Im asking is if there is a need for me to specify characters which are say between the position 100-106 then it kind of becomes difficult for me to mention the dots.

Code:
grep -A5 "^0[67][MV].\{22\}003529" file

is really fast compared to awk
# 5  
Old 04-07-2017
NR is the actual line No., for instance 17. You want six additional lines printed, so L becomes 23, and awk will print (default action if a pattern is TRUE) as long as NR is less than 23.

My awk versions don't seem to accept what is called a "bound" (c.f. man regex); you need to try it on your own, and YMMV.
Or, compose the pattern from a leading regex match, and a substr () match afterwards.
# 6  
Old 04-07-2017
Quote:
Originally Posted by dsid
I had tried your command with a little tweak
Code:
awk '/^0[67][MV].*003529/'

and the output I got was what it was supposed to give. However, it isn't exactly suitable for me as I am looking for only those lines which have 003529 in the 26-31 position.
Also there is either a 06M or 06V or 07M or 07V in the line that I need to search. When the regex matches, it should print the next 5 lines, including $0, in total 6 lines.
Hope I am clear now.
Hello dsid,

Could you please try following and let me know if this helps you.
Code:
awk '/^0[67][MV]/ && substr($0,26,1)==0 && substr($0,27,1)==0 && substr($0,28,1)==3 && substr($0,29,1)==5 && substr($0,30,1)==2 && substr($0,31,1)==9'  Input_file

EDIT: In case you want to print the next 6 lines after the matching line, you could do the following then.
Code:
awk '/^0[67][MV]/ && substr($0,26,1)==0 && substr($0,27,1)==0 && substr($0,28,1)==3 && substr($0,29,1)==5 && substr($0,30,1)==2 && substr($0,31,1)==9{;print;A=6;next} A>0{print;A--}'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 04-07-2017 at 07:10 AM..
# 7  
Old 04-07-2017
Quote:
Originally Posted by RavinderSingh13
Hello dsid,

Could you please try following and let me know if this helps you.
Code:
awk '/^0[67][MV]/ && substr($0,26,1)==0 && substr($0,27,1)==0 && substr($0,28,1)==3 && substr($0,29,1)==5 && substr($0,30,1)==2 && substr($0,31,1)==9'  Input_file

EDIT: In case you want to print the next 6 lines after the matching line, you could do the following then.
Code:
awk '/^0[67][MV]/ && substr($0,26,1)==0 && substr($0,27,1)==0 && substr($0,28,1)==3 && substr($0,29,1)==5 && substr($0,30,1)==2 && substr($0,31,1)==9{;print;A=6;next} A>0{print;A--}'  Input_file

Thanks,
R. Singh
both of your commands work, however, I had to tweak your second command and put a '5' instead of 6 cos it was printing that extra line at the end. can you please explain the whole command and why are individual substring() needed.


Quote:
Originally Posted by RudiC
NR is the actual line No., for instance 17. You want six additional lines printed, so L becomes 23, and awk will print (default action if a pattern is TRUE) as long as NR is less than 23.

My awk versions don't seem to accept what is called a "bound" (c.f. man regex); you need to try it on your own, and YMMV.
Or, compose the pattern from a leading regex match, and a substr () match afterwards.
After a lot of head scratching, I finally managed to find a way out using sed
Code:
sed -n '/0[67][MV].\{22\}003529/,+5p' 2017087222049-26-S-T-M-0_SW.inc

I couldn't figure out a way to mention specific sets using regex in awk (highlighted in RED). The only thing I could figure out was
Code:
awk '/\y003529\y/ {L = NR + 6} NR < L'

but that wasn't doing the trick either
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