Grep

Hi,
I have a file as:

IIN*00*00*ZZ*NDC *ZZ*11847*090520*0200*U*00401*000000001*0*P*>~GS*HP*NDC STD REMIT*1184722779*20090520*0200*63*X*004010X091A1~ST*835*10001~BPR*I*252480.19*C*CHK************20090515~TRN*1*0010855862*1952931460*12427008150981~DTM*405*20090514~N1*PR*CALIFORNIA*XV*H00~N3*ireland~

I want to extract 'CALIFORNIA' from the above one line file. The file will contain 'N1*PR*------' . I want to extract the string after N1*PR*.

Appreciate your help.

Regards,
Anil

If it's a fixed-field record with '*' as the separator, you could use

awk -F'*' '{print $49}' file

Otherwise, if you only want the part after 'N1*PR*', this Perl snippet might help:

perl -ne 'print $1,"\n" if /N1\*PR\*(.*?)\*/;' file

if you have Python

#!/usr/bin/env python
for line in open("file"):
    if "N1*PR*" in line:
        ind=line.index("N1*PR*")
        print line[ind+6:].split("*")[0]

output

# ./test.py
CALIFORNIA

Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

VIDYA

Thanks. Perl one-liner worked.:slight_smile: