INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Pattern at variable position within line

Pattern at variable position within line

(OP)
I looked through several pages for an example, but without success, so here's my situation. I'm using grep to locate records with the keyword "PCN". I want to pipe that result to awk to find/list/print the next field following the keyword. However, the keyword (PCN) can occur at almost any position within the record (never the first position, but that hardly matters).

Just so you know the purpose, this is scanning across a printed report. The report supplier is going to increase the size of the field following the PCN and I want to be able to detect the first time they send using the new format so that we can do some validation. The PCN is an account number, and existing accounts will continue to use their shorter number, so locating the new accounts has great value to the testers and auditors. Thanks!

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than agile big data clouds)


RE: Pattern at variable position within line

Hi

Could we see some sample input please ?

For now I am not sure whether you are talking about single line or multi-line records. ( Actually, I am not sure neither about the need for Awk. )

Feherke.
feherke.ga

RE: Pattern at variable position within line

(OP)
Here is some sample data (output of the grep). Obviously, real account numbers have been replaced. In this example, SCOTT J with account number FFFFFFFFFFFF has the new account number. Assuming they provide the revised report in the way they said they would. At this point, I'm just interested in getting the field after the PCN which I will then throw into a file for further analysis (length comes to mind, but since they might change other things in the report format, who knows what else I'll need to check). Since some people have 0 to 3 middle initials, or two last names, I can't use positionals like $1, $2, etc. I suppose I could "throw away" everything before the PCN and then take the first field.....lemme try that.

MCDSDFUZ0701anyq0000.txt: NAME RAYKIN I PCN 00000AAAAAA -04 SERVICE FROM 20150521 THRU 20150521
MCDSDFUZ0701anyq0000.txt: NAME RIVELL D F PCN 00000BBBBBB -06 SERVICE FROM 20150521 THRU 20150521
MCDSDFUZ0701anyq0000.txt: NAME RIZZO R M PCN CCCCCCCCCCC -05 SERVICE FROM 20150519 THRU 20150519
MCDSDFUZ0701anyq0000.txt: NAME RIZZO R M PCN CCCCCCCCCCC -07 SERVICE FROM 20150526 THRU 20150526
MCDSDFUZ0701anyq0000.txt: NAME RIZZO R M PCN CCCCCCCCCCC -08 SERVICE FROM 20150520 THRU 20150520
MCDSDFUZ0701anyq0000.txt: NAME SAHAR Q N PCN DDDDDDDDDDD -02 SERVICE FROM 20150515 THRU 20150515
MCDSDFUZ0701anyq0000.txt: NAME SALGUERO K Y PCN EEEEEEEEEEE -01 SERVICE FROM 20150414 THRU 20150414
MCDSDFUZ0701anyq0000.txt: NAME SCOTT J PCN FFFFFFFFFFFF-05 SERVICE FROM 20150520 THRU 20150520
MCDSDFUZ0701anyq0000.txt: NAME SINH S PCN GGGGGGGGGGG -01 SERVICE FROM 20150603 THRU 20150603
MCDSDFUZ0701anyq0000.txt: NAME STONE I PCN HHHHHHHHHHH -33 SERVICE FROM 20150429 THRU 20150429
MCDSDFUZ0701anyq0000.txt: NAME STONE I PCN HHHHHHHHHHH -33 SERVICE FROM 20150429 THRU 20150429
MCDSDFUZ0701anyq0000.txt: NAME STONE I PCN HHHHHHHHHHH -37 SERVICE FROM 20150513 THRU 20150513

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than agile big data clouds)


RE: Pattern at variable position within line

Hi

Well, this is close to my estimation and my concern regarding the need for Awk stands.

grep ( the GNU implementation at least ) can do quite a lot here :

CODE --> command line

master # grep 'PCN \S\{12\}' MCDSDFUZ0701anyq0000.txt
NAME SCOTT J PCN FFFFFFFFFFFF-05 SERVICE FROM 20150520 THRU 20150520

master # grep -o 'PCN \S\{12\}' MCDSDFUZ0701anyq0000.txt
PCN FFFFFFFFFFFF

master # grep -o 'PCN \S\{12\}' MCDSDFUZ0701anyq0000.txt | grep -o '\S\{12\}'
FFFFFFFFFFFF 

But as you asked in the Awk forum :

CODE --> command line

master # awk '{for(i=1;i<NF;i++)if($i=="PCN"&&length($(i+1))>=12)print substr($(i+1),1,12)}' MCDSDFUZ0701anyq0000.txt
FFFFFFFFFFFF 

Tested with gawk and mawk.

Feherke.
feherke.ga

RE: Pattern at variable position within line

(OP)
Well, thanks for teaching me some things that I didn't know about grep, here in the awk forum !

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than agile big data clouds)


RE: Pattern at variable position within line

(OP)
Aha! We don't have the -o option for grep in SunOS 5.10 (22 Jun 2005). YIKES !!
We will be doing a server upgrade later this year, but for now, we don't have that grep option in our version of Solaris, so thank you feherke for the awk solution as well.

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than agile big data clouds)


RE: Pattern at variable position within line

Hi

Doh. And I was about to suggest another solution, implying a single grep call :

CODE --> command line

master # grep -oP '(?<=PCN )\S{12}' MCDSDFUZ0701anyq0000.txt
FFFFFFFFFFFF 
But PCRE is experimental even in grep, probably not found in other implementations.

By the way, negative look-behind assertion is also available in ack, but probably not popular on Unix systems :

CODE --> command line

master # ack -o '(?<=PCN )\S{12}' MCDSDFUZ0701anyq0000.txt
FFFFFFFFFFFF 

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close