2

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?

2 Answers 2

3
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
18
  • 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 ? Commented Jun 24, 2014 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. Commented Jun 24, 2014 at 6:52
  • NUM[$a] var is for data storage Commented Jun 24, 2014 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... Commented Jun 24, 2014 at 6:59
  • 1
    It works. I will apply this to some files in my dir. Commented Jun 24, 2014 at 7:06
3

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.*/'
4
  • Thanks for straightening up that input, Gnouc. I was getting a little cross-eyed. Commented Jun 24, 2014 at 5:45
  • I tried above code but it didn't work out. Commented Jun 24, 2014 at 6:39
  • What is your OS and grep version? Commented Jun 24, 2014 at 6:43
  • Windows and version 4.1.2(1) Commented Jun 24, 2014 at 6:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.