The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Strip one line from 2 blank lines in a file tipsy Shell Programming and Scripting 6 06-23-2008 08:14 AM
strip first 4 and last 2 lines from a file using perl meghana Shell Programming and Scripting 10 02-01-2008 09:01 PM
How do you buffer streamed lines? jjinno Shell Programming and Scripting 14 07-19-2007 03:36 PM
Strip 3 header lines and 4 trailer lines ganesh123 Shell Programming and Scripting 9 03-10-2007 05:15 PM
Output every certain lines. munnabhai1 Shell Programming and Scripting 1 04-06-2006 01:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2008
sapedi sapedi is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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 (permalink)  
Old 11-18-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Try it first without the colored fragment of the code
Code:
awk '/^ISA/||/^GS/{next}{print}' file > newfile && mv newfile > file
  #3 (permalink)  
Old 11-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool

Would two grep's to exclude work?

Code:
grep -v "^ISA" file75 | grep -v "^GS"
  #4 (permalink)  
Old 11-18-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,369
Or one grep
Code:
grep -v '^[ISA|GS]' file
  #5 (permalink)  
Old 11-18-2008
sapedi sapedi is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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 (permalink)  
Old 11-18-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,369
Try awk:
Code:
awk -F'~' '{for(i=1;i<NF;i++){if($i !~ /^[ISA|GS]/)printf $i FS}}'
  #7 (permalink)  
Old 11-19-2008
sapedi sapedi is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0