Find the answer to your Linux question:
Results 1 to 6 of 6
Hi! I relatively new to using linux. I have 2 files I need to join the FILE1 to FILE2 based on the first field(ie abc...) , if there is a ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    4

    Joining two files

    Hi! I relatively new to using linux. I have 2 files I need to join the FILE1 to FILE2 based on the first field(ie abc...) , if there is a match on FILE2 then if see if FILE1 field2 is between the range in FILE2? If its between range then continue if its outside of range then fail script
    Please help! Thanks! See file example below


    FILE1
    abc 33
    def 22
    xyz 15

    FILE2
    abc 25 70
    ddw 12 55
    def 20 30
    qwe 33 77
    xyz 20 55

  2. #2
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Welcome to the forums!

    Is this a homework question?
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    4

    I wish it was only homework....

    Its actually some monitoring I'm trying to put in our system. I just mocked up the data instead of using our actual data. I wrote a sqlplus that creates the FILE1 and need to compare it FILE2 and if there is a match(on the key) then check to see if its within the allowable range. If yes move on to the next value to check, if not then fail the script. In which I'll add in my logic to page me.

    Thanks!

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    awk 'FNR == NR {key[$1]=$1;num[$1]=$2;next;}
    {
      for ( i in key ) {
        if ($1 == i && ( num[i] < $2 || num[i] > $3 )) {
          print "This line is out of range: "$0
        }
      }
    }' "FILE1" "FILE2"
    Regards

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    I like Franklin52's solution because it is all in one piece. Here's a similar solution, but using a Linux utility:
    Code:
    #!/usr/bin/env sh#!/usr/bin/env sh
    
    # @(#) s1       Demonstrate join.
    
    set -o nounset
    echo
    
    debug=":"
    debug="echo"
    
    ## Use local command version for the commands in this demonstration.
    
    echo "(Versions of codes used in this script with local utility \"version\")"
    version bash join awk
    
    echo
    
    join FILE1 FILE2 |
    awk '
    $2 < $3 || $2 > $4      { print "Out of bounds: " $0 }
    '
    
    exit 0
    Producing:
    Code:
    % ./s1
    
    (Versions of codes used in this script with local utility "version")
    GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
    join (coreutils) 5.2.1
    GNU Awk 3.1.4
    
    Out of bounds: xyz 15 20 55
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Just Joined!
    Join Date
    Sep 2007
    Posts
    4

    Smile It worked

    Thanks you both for your suggestions, I used Franklin52's solution. Now I will work on incorporating this into the rest of my processing.

    Thanks again. My first experience in a forum has been most helpful.

Posting Permissions

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