Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2011
    Posts
    2

    Unanswered: need help shell script

    i have 2 files, a.txt and b.txt, delimited with character |
    i want to combine 2 files and the result like file output.txt
    if there's missing field ini file b.txt, than the result is 'ERR'

    please help me, how shell script for this case

    thank you

    file a.txt :
    aaaa | 0001
    bbbb | 0004
    cccc | 0001
    dddd | 0003
    eeee | 0003
    ffff | 0005
    gggg | 0002
    hhhh | 0002
    iiii | 0006
    jjjj | 0001
    kkkk | 0001
    llll | 0004
    mmmm | 0005
    nnnn | 0005


    file b.txt :
    0001 | red
    0002 | yellow
    0003 | blue
    0004 | black
    0005 | green


    output :
    aaaa | red
    bbbb | black
    cccc | red
    dddd | blue
    eeee | blue
    ffff | green
    gggg | yellow
    hhhh | yellow
    iiii | ERR
    jjjj | red
    kkkk | red
    llll | black
    mmmm | green
    nnnn | green

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    #!/bin/ksh                                 
    while read a pipe b                        
    do                                         
            color=`grep $b b.txt|cut -f2 -d "|"`
            if [ "ERR$color" = "ERR" ]         
            then                               
             color="ERR"                       
            fi                                 
            echo $a "|" $color                 
    done <a.txt
    Last edited by kitaman; 07-21-11 at 15:25. Reason: ` missing at end of line 4

  3. #3
    Join Date
    Jul 2011
    Posts
    2
    thank you for help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •