How to Strip lines off Streamed EDI Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Strip lines off Streamed EDI Output
# 1  
Old 11-18-2008
How to Strip lines off Streamed EDI Output

Attached is a streamed EDI ANSI X12 output where the segment terminator/delimiter is a tilde ~ character.

Is it possible to do the following pseudo-code in a unix script (using either sed, awk and/or grep)?
Open file StreamedOutput.txt
Search for ISA and delete the data up to the tilde ~ char
Search for GS and delete the data up to the tilde ~ char
And write the ‘middle' bit of data to the same filename.

In other words I want to strip off the first (ISA) and last segment (GS) of this file all up to the tilde and leave me the bit of data in the middle.

I tried to use the following script but not having much joy;
x=`grep -n "ISA" StreamedOutput | cut -d "~" -f1`
y=`grep -n "GS" StreamedOutput | cut -d "~" -f1`
a=`echo ${x} + 1 | bc`
b=`echo ${y} -1 | bc`
echo $x $y $a $b
sed -ne '${a},${b}p' StreamedOutput


If I unstreamed the input data it looks like;
ISA*HDR*START*US*0050000013~
BA1*Y**A*US*0050000013***TN****Federal Express~
YNQ*RZ*Y~
YNQ*QQ*Y~
DTM*274*081015~
N1*EX*Smith & Nephew, Inc.*24*510123924RT~
N2*1*9013995343~
N3*EAST HOLMES RD~
N4*Memphis*TN*38118*US~
N1**CAREY STEVE~
N3*1720 EAST BLACKHAWK DR~
N4*PHOENIX*AZ*85024*US~
N1*FW*Federal Express*24~
N3*123 Main~
N4*MEMPHIS*TN**US~
L13*A*9021.10.0050***10*20****K*0*Bone plates, screws & nails & other inte*OS*D~
L13*A*9021.31.0000***10*1000****K*0*Artificial joints & parts & accessories*OS*F~
L13*A*9021.31.0000***10*1000****K*0*Artificial joints & parts & accessories*OS*D~
GS*TRL*END*US*0050000013~

And the result I want is;
BA1*Y**A*US*0050000013***TN****Federal Express~
YNQ*RZ*Y~
YNQ*QQ*Y~
DTM*274*081015~
N1*EX*Smith & Nephew, Inc.*24*510123924RT~
N2*1*9013995343~
N3*EAST HOLMES RD~
N4*Memphis*TN*38118*US~
N1**CAREY STEVE~
N3*1720 EAST BLACKHAWK DR~
N4*PHOENIX*AZ*85024*US~
N1*FW*Federal Express*24~
N3*123 Main~
N4*MEMPHIS*TN**US~
L13*A*9021.10.0050***10*20****K*0*Bone plates, screws & nails & other inte*OS*D~
L13*A*9021.31.0000***10*1000****K*0*Artificial joints & parts & accessories*OS*F~
L13*A*9021.31.0000***10*1000****K*0*Artificial joints & parts & accessories*OS*D~
# 2  
Old 11-18-2008
Try it first without the colored fragment of the code
Code:
awk '/^ISA/||/^GS/{next}{print}' file > newfile && mv newfile > file

# 3  
Old 11-18-2008
Tools

Would two grep's to exclude work?

Code:
grep -v "^ISA" file75 | grep -v "^GS"

# 4  
Old 11-18-2008
Or one grep
Code:
grep -v '^[ISA|GS]' file

# 5  
Old 11-18-2008
Thanks for the replies, however how will your code change if the data was streamed (i.e. one long line of data in this case) so the delimiter separating the segments is the tilde ~ char.
# 6  
Old 11-18-2008
Try awk:
Code:
awk -F'~' '{for(i=1;i<NF;i++){if($i !~ /^[ISA|GS]/)printf $i FS}}'

# 7  
Old 11-19-2008
Hi Danmero, would it be too much to ask for a breakdown of your code and it's intentions/meaning as I tried to code using a combination of grep and sed? Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

EDI File Parser

I've one EDI file which is to be parsed into 7 different file. I managed to extract required segments for a file(HEADER) to a separate file(sample3.dat) and is given below. $ cat sample3.dat REF*EI*273543997~ REF*2U*HELLO~ REF*G2*77685|132~ CLM*1000*0.00***12>B>1*N*A*Y*I~ CN1*05~... (5 Replies)
Discussion started by: ashokv3
5 Replies

2. UNIX for Dummies Questions & Answers

Strip out number from wc output?

I am trying to output two command substitutions echo "$(command) $(command)" with a single echo however when using wc -l within the second substitution, that substitution, instead of appearing in order at the end of the line output, it overwrites the beginning of the output line. I've tried... (2 Replies)
Discussion started by: somegeek
2 Replies

3. SCO

Add memory and EDI hard disk space

Dear all I would like to know if you have face to the problems as add 1. 256 MB DDR Ram memory 2. IDE 40 GB slaved hard-disk my current machine is GA-SIML Rev 1.0, 1.6 GHz CPU, 1GB HDD, 256 MB DDR-Ram memory, as I add another 256 DDR-Ram, and 40 GB Slaved EDI Hard disk it, the systems... (1 Reply)
Discussion started by: TinhNhi
1 Replies

4. Shell Programming and Scripting

parsing issue with edi file

Hello, We have edi files we need to do some extra parsing on. There is a line that shows up that looks like this: GE|8,845|000000000 We need to parse the file, find the line ( that begins with GE "^GE" ), and remove the comma(s). What is the easiest way to do that ? I know I can grab... (5 Replies)
Discussion started by: fwellers
5 Replies

5. Shell Programming and Scripting

KSH Output/Strip portion of file in HP-UX

I have a file, we'll call it file.txt. It has thousands of lines of all kinds of output at any given time (ie. foo bar foo bar) I need to copy out just a portion of the file from Point-A to Point-B. I'd like to save off just that portion to a file called test123xyz.txt. How do I do that? ... (7 Replies)
Discussion started by: austin881
7 Replies

6. Shell Programming and Scripting

Strip one line from 2 blank lines in a file

Hi Is there any command to scan thru a file looking for 2 consecutive blank lines and if any remove one of them. Please let me know. Regards, Tipsy (6 Replies)
Discussion started by: tipsy
6 Replies

7. Shell Programming and Scripting

strip first 4 and last 2 lines from a file using perl

Hi I have a file from which i need to remove the first 4 and the last 2 lines.. i know how to do it with sed but i need to do it in a perl script.. can you please help me how to do that. Thanks (10 Replies)
Discussion started by: meghana
10 Replies

8. Shell Programming and Scripting

How do you buffer streamed lines?

If I am running a cat|awk|sed on a file to get a bunch of numbers between 1 and 500 billion, how can I stay in stream, and still compare "this value" with "the previous value"? Really I could do this all in Perl, but if I can get it to work in-stream, its gonna be TONS faster... even if I can... (14 Replies)
Discussion started by: jjinno
14 Replies

9. Shell Programming and Scripting

Strip 3 header lines and 4 trailer lines

Hello friends, I want to remove 3 header lines and 4 trailer lines, I am using following , is it correct ? sed '1,3d';'4,$ d' filename (9 Replies)
Discussion started by: ganesh123
9 Replies
Login or Register to Ask a Question