Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am new to linux shell and I had a hard time thinking of the solution to this. Can somebody help how to parse multiple strings (data) in a file? For below file, I want to parse all the values next to BIG*20021208*00001** which are the following: A999, A1000, 1001.

ST*810*0001
BIG*20021208*00001**A9
99*N1*ST*XYZ Test Corporation*9*122334455
N3*987 Freeway Dr.
N4*New York*NY*98765
N1*BT*ABC Test Company*9*122334455
N3*123 Highway Street
N4*Los Angeles*CA*12345
ITD*01*3*2**30**30*****60
FOB*PP
IT1**1*EA*200**UA*EAN
PID*F****Lamp
IT1**4*EA*50**UA*EAN
PID*F****Chair
TDS*2000
CAD*****Routing
ISS*30*CA
CTT*50
SE*19*0001
ST*810*0002
BIG*20021208*00001**A1000
N1*ST*XYZ Test Corporation*9*122334455
N3*987 Freeway Dr.
N4*New York*NY*98765
N1*BT*ABC Test Company*9*122334455
N3*123 Highway Street
N4*Los Angeles*CA*12345
ITD*01*3*2**30**30*****60
FOB*PP
IT1**1*EA*200**UA*EAN
PID*F****Lamp
IT1**4*EA*50**UA*EAN
PID*F****Chair
TDS*2000
CAD*****Routing
ISS*30*CA
CTT*50
SE*19*0002
ST*810*0003
BIG*20021208*00001**10
01N1*ST*XYZ Test Corporation*9*122334455
N3*987 Freeway Dr.
N4*New York*NY*98765
N1*BT*ABC Test Company*9*122334455
N3*123 Highway Street
N4*Los Angeles*CA*12345
ITD*01*3*2**30**30*****60
FOB*PP
IT1**1*EA*200**UA*EAN
PID*F****Lamp
IT1**4*EA*50**UA*EAN
PID*F****Chair
TDS*2000
CAD*****Routing
ISS*30*CA
CTT*50
SE*19*0003

Now, as you can see in the BIG segment, A999 value were separated already(no longer intact) . Then my problem now is when I get the data (e.g.A999) it will only generated this A9 and not the whole value which is A999.Is there any workaround for this?

share|improve this question
add comment

2 Answers 2

sed '/BIG.20021208.00001/!d;s/.*\*//' <<\DATA
    ST*810*0001
    BIG*20021208*00001**A999
    N1*ST*XYZ Test Corporation*9*122334455
    N3*987 Freeway Dr.
    N4*New York*NY*98765
    N1*BT*ABC Test Company*9*122334455
    N3*123 Highway Street
    N4*Los Angeles*CA*12345
    ITD*01*3*2**30**30*****60
    FOB*PP
    IT1**1*EA*200**UA*EAN
    PID*F****Lamp
    IT1**4*EA*50**UA*EAN
    PID*F****Chair
    TDS*2000
    CAD*****Routing
    ISS*30*CA
    CTT*50
    SE*19*0001
    ST*810*0002
    BIG*20021208*00001**A1000
    N1*ST*XYZ Test Corporation*9*122334455
    N3*987 Freeway Dr.
    N4*New York*NY*98765
    N1*BT*ABC Test Company*9*122334455
    N3*123 Highway Street
    N4*Los Angeles*CA*12345
    ITD*01*3*2**30**30*****60
    FOB*PP
    IT1**1*EA*200**UA*EAN
    PID*F****Lamp
    IT1**4*EA*50**UA*EAN
    PID*F****Chair
    TDS*2000
    CAD*****Routing
    ISS*30*CA
    CTT*50
    SE*19*0001
    ST*810*0002
    BIG*20021208*00001**A1000
    N1*ST*XYZ Test Corporation*9*122334455
    N3*987 Freeway Dr.
    N4*New York*NY*98765
    N1*BT*ABC Test Company*9*122334455
    N3*123 Highway Street
    N4*Los Angeles*CA*12345
    ITD*01*3*2**30**30*****60
    FOB*PP
    IT1**1*EA*200**UA*EAN
    PID*F****Lamp
    IT1**4*EA*50**UA*EAN
    PID*F****Chair
    TDS*2000
    CAD*****Routing
    ISS*30*CA
    CTT*50
    SE*19*0002
    ST*810*0003
    BIG*20021208*00001**1001
    N1*ST*XYZ Test Corporation*9*122334455
    N3*987 Freeway Dr.
    N4*New York*NY*98765
    N1*BT*ABC Test Company*9*122334455
    N3*123 Highway Street
    N4*Los Angeles*CA*12345
    ITD*01*3*2**30**30*****60
    FOB*PP
    IT1**1*EA*200**UA*EAN
    PID*F****Lamp
    IT1**4*EA*50**UA*EAN
    PID*F****Chair
    TDS*2000
    CAD*****Routing
    ISS*30*CA
    CTT*50
    SE*19*0003
DATA

That yields only these results:

A999
A1000
1001
share|improve this answer
    
Still it didn't work, do you think I have a wrong syntax with this line of code: NUM[$a]=sed '/BIG.20021208.00001/!d;s/.*\*//' < /tmp/temp.file.txt ? –  Kredian Jun 24 at 6:49
    
@Michelle - definitely did it - the results are copy-pasted. But your thing looks correct as well. I'm not sure what the NUM part does though. –  mikeserv Jun 24 at 6:52
    
NUM[$a] var is for data storage –  Kredian Jun 24 at 6:54
    
@Michelle - I've just edited it to show the exact command I used verbatim - and those are the results I received. I didn't include it before because it was so long... –  mikeserv Jun 24 at 6:59
1  
It works. I will apply this to some files in my dir. –  Kredian Jun 24 at 7:06
show 13 more comments

With GNU grep, you can try:

$ grep -oP 'BIG\*20021208\*00001\*\*\K.*' file
A999
A1000
1001

If your grep version does not support PCRE, you can use perl instead:

$ perl -nle 'print $& if /BIG\*20021208\*00001\*\*\K.*/'
share|improve this answer
    
Thanks for straightening up that input, Gnouc. I was getting a little cross-eyed. –  mikeserv Jun 24 at 5:45
    
I tried above code but it didn't work out. –  Kredian Jun 24 at 6:39
    
What is your OS and grep version? –  Gnouc Jun 24 at 6:43
    
Windows and version 4.1.2(1) –  Kredian Jun 24 at 6:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.