Last active
          August 29, 2015 14:27 
        
      - 
      
- 
        Save bertrandmartel/771b2371c0050b658fcd to your computer and use it in GitHub Desktop. 
    [ STACKOVERFLOW ] format text which match specific pattern showing it only with grep notation and awk notation
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| IFS=$'\n' #line delimiter | |
| #empty your output file | |
| cp /dev/null "$2" | |
| for i in $(cat "$1"); do | |
| is_gin=`echo $i | grep "GIN+"` | |
| is_lin=`echo $i | grep "LIN+"` | |
| is_qty=`echo $i | grep "QTY+"` | |
| is_rff=`echo $i | grep "RFF+"` | |
| if [ ! -z "$is_gin" ]; then | |
| #gin=`echo $i | grep -o '[0-9]*'` | |
| gin=`echo $i | awk 'match($0,/[0-9]{1,}/) {print substr($0,RSTART,RLENGTH)}'` | |
| newline=0 | |
| linecontent="GIN${gin}," | |
| elif [ ! -z "$is_lin" ]; then | |
| #linreq=`echo $i | sed 's/.*++//g' | grep -o '[0-9]*'` | |
| linreq=`echo $i | sed 's/.*++//g' | awk 'match($0,/[0-9]{1,}/) {print substr($0,RSTART,RLENGTH)}'` | |
| if [ -z "$linecontent" ]; then | |
| linecontent="GIN${gin},LIN${linreq}," | |
| else | |
| linecontent="${linecontent}LIN${linreq}," | |
| fi | |
| elif [ ! -z "$is_qty" ]; then | |
| #qty=`echo $i | grep -o ':[0-9]*' | sed -e 's/://g'` | |
| qty=`echo $i | awk 'match($0,/:[0-9]{1,}/) {print substr($0,RSTART,RLENGTH)}' | sed -e 's/://g'` | |
| linecontent="${linecontent}QTY${qty}," | |
| elif [ ! -z "$is_rff" ]; then | |
| #rff=`echo $i | grep -o '[0-9]*-[0-9]*-[0-9]*'` | |
| rff=`echo $i | awk 'match($0,/[0-9]{1,}-[0-9]{1,}-[0-9]{1,}/) {print substr($0,RSTART,RLENGTH)}'` | |
| linecontent="${linecontent}RFF${rff}" | |
| echo $linecontent >> "$2" | |
| linecontent="" | |
| fi | |
| done | |
| cat $2 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | UNB+UNOA:2+5060096369998:14+[BUYER GLN]:14+150310:0105+000023++DESADV' | |
| UNH+0000001+DESADV:D:96A:UN:EAN005' | |
| BGM+351+[INVOICE NUMBER]' | |
| DTM+137:20150309:102' | |
| DTM+11:20150309:102' | |
| DTM+63:20150310:102' | |
| NAD+BY+[BUYER GLN]::9++[BUYER NAME AND ADDRESS]' | |
| NAD+SU+5060096369998::9++GARDNERS BOOKS LTD.+1 WHITTLE DRIVE:WILLINGDON DROVE:EASTBOURNE+++BN23 6QH+GB' | |
| NAD+DP+[SHIPPING GLN]::9++[SHIPPING NAME AND ADDRESS]' | |
| CPS+1' | |
| PAC+3++PK' | |
| MEA+PD+AAB+KGM:11' | |
| MEA+PD+HT+MMT:460' | |
| MEA+PD+WD+MMT:310' | |
| MEA+PD+LN+MMT:235' | |
| PCI+33E' | |
| GIN+BJ+305060096368359186' | |
| LIN+1++9780006755227:EN' | |
| PIA+1+0006755224:IB' | |
| IMD+F+BST+:::DOGSBODY/JONES, DIANA WYNNE' | |
| QTY+12:1' | |
| RFF+ON:124705572-15-1' | |
| LIN+2++9780007245826:EN' | |
| PIA+1+0007245823:IB' | |
| IMD+F+BST+:::VINTAGE AFFAIR/WOLFF, ISABEL' | |
| QTY+12:3' | |
| RFF+ON:124705572-62-1' | |
| LIN+3++9780007250608:EN' | |
| PIA+1+0007250606:IB' | |
| IMD+F+BST+:::MYSTERY OF THE BLUE TRAIN COMIC STR' | |
| QTY+12:1' | |
| RFF+ON:124705572-48-1' | |
| PAC+3++PK' | |
| MEA+PD+AAB+KGM:11' | |
| MEA+PD+HT+MMT:460' | |
| MEA+PD+WD+MMT:310' | |
| MEA+PD+LN+MMT:235' | |
| PCI+33E' | |
| GIN+BJ+305060096368359193' | |
| LIN+4++9780091950309:EN' | |
| PIA+1+0091950309:IB'IMD+F+BST+:::LOVER/JORDAN, NICOLE' | |
| QTY+12:1' | |
| RFF+ON:124705572-98-1' | |
| LIN+5++9780099453956:EN' | |
| PIA+1+0099453959:IB'IMD+F+BST+:::FLANDERS PANEL/PEREZ-REVERTE, ARTUR' | |
| QTY+12:1' | |
| RFF+ON:124705572-22-1' | |
| LIN+6++9780099481379:EN' | |
| PIA+1+0099481375:IB' | |
| IMD+F+BST+:::MYSTERIOUS FLAME OF QUEEN LOANA/ECO' | |
| QTY+12:2' | |
| RFF+ON:124705572-36-1' | |
| CNT+1:9' | |
| CNT+2:6' | |
| UNT+695+0000001' | |
| UNZ+1+000023' | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | GIN305060096368359186,LIN9780006755227,QTY1,RFF124705572-15-1 | |
| GIN305060096368359186,LIN9780007245826,QTY3,RFF124705572-62-1 | |
| GIN305060096368359186,LIN9780007250608,QTY1,RFF124705572-48-1 | |
| GIN305060096368359193,LIN9780091950309,QTY1,RFF124705572-98-1 | |
| GIN305060096368359193,LIN9780099453956,QTY1,RFF124705572-22-1 | |
| GIN305060096368359193,LIN9780099481379,QTY2,RFF124705572-36-1 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment