Find the answer to your Linux question:
Results 1 to 4 of 4
I have two files file1.txt $1 $2 $3 $4 $5 $6 $7 $8 Code: 1234567|iufgt|iuoy|iout|white |black |red |90879 1234567|iufgt|iuoy|iout|green |pink |blue |90879 1234567|iufgt|iuoy|iout|orange|purple|magenta|9087 9 1234567|iufgt|iuoy|iout|yellow |violet|grey |90879 we have to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    1

    Comparing two files


    I have two files


    file1.txt
    $1 $2 $3 $4 $5 $6 $7 $8

    Code:
    1234567|iufgt|iuoy|iout|white |black |red |90879
    1234567|iufgt|iuoy|iout|green |pink |blue |90879
    1234567|iufgt|iuoy|iout|orange|purple|magenta|9087 9
    1234567|iufgt|iuoy|iout|yellow |violet|grey |90879



    we have to consider here $5 , $6 , $7 for our search




    file2.txt

    $1 $2 $3 $4 $5 $6

    Code:
    1234567|grey|iuoy|iout|iufgt|90879
    1239877|magenta|iuoy|iout|iufgt|90879
    1733267|blue|iuoy|iout|iufgt|90879
    1232677|red|iuoy|iout|iufgt|90879
    1239567|white|iuoy|iout|iufgt|90879
    1238727|green|iuoy|iout|iufgt|90879
    1237247|orange|iuoy|iout|iufgt|90879
    1236397|yellow|iuoy|iout|iufgt|90879
    1232947|pink|iuoy|iout|iufgt|90879
    1230247|black|iuoy|iout|iufgt|90879
    1234037|violet|iuoy|iout|iufgt|90879
    1238037|purple|iuoy|iout|iufgt|90879
    1237897||iuoy|iout|iufgt|90879
    1238797||iuoy|iout|iufgt|90879
    1239997||iuoy|iout|iufgt|90879
    here we should take only $2 for comparison. As you can most of the $2 field records has value and some do not have value



    Question:

    I want to take the fields $5 , $6 , $7 from file 1 and compare it with $2 field from file 2. and the rsult should be like this


    Code:
    if $5 (file1) =$2 (file2) then replace $5 (file1) with $1 of (file2)
    if $6 (file1) =$2 (file2) then replace $6 (file1) with $1 of (file2)
    if $7 (file1) =$2 (file2) then replace $7 (file1) with $1 of (file2)
    the final output will look like this

    Actual file1.txt (before running the code)

    $1 $2 $3 $4 $5 $6 $7 $8

    Code:
    1234567|iufgt|iuoy|iout|white |black |red |90879
    1234567|iufgt|iuoy|iout|green |pink |blue |90879
    1234567|iufgt|iuoy|iout|orange|purple|magenta|9087 9
    1234567|iufgt|iuoy|iout|yellow|violet|grey |90879


    FIle1.txt after running the above said condition


    $1 $2 $3 $4 $5 $6 $7 $8

    Code:
    1234567|iufgt|iuoy|iout|1239567 |1230247 |1232677 |90879
    1234567|iufgt|iuoy|iout|1238727 |1232947 |1733267 |90879
    1234567|iufgt|iuoy|iout|1237247 |1238037 |1239877 |90879
    1234567|iufgt|iuoy|iout|1236397 |1234037 |1234567 |90879

    so the field $5 , $6 , $7 should get replaced from the matched valued of $1(file1)


    Please advice how it can do done. if u can do it in join , awk or nawk it would be really helpful.

  2. #2
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145

    Post

    From awk man page:

    <...>

    The -F fs option
    defines the input field separator to be the regular expression fs

    <...>

    An action is a sequence of statements. A statement can be one of the
    following:

    if( expression ) statement [ else statement ]
    while( expression ) statement
    for( expression ; expression ; expression ) statement
    for( var in array ) statement
    do statement while( expression )
    break
    continue
    { [ statement ... ] }
    expression # commonly var = expression
    print [ expression-list ] [ > expression ]
    printf format [ , expression-list ] [ > expression ]
    return [ expression ]
    next # skip remaining patterns on this input line
    nextfile # skip rest of this file, open next, start at top
    delete array[ expression ]# delete an array element
    delete array # delete all elements of array
    exit [ expression ] # exit immediately; status is expression
    That's everything you need. The solution is:

    1. Extract second field using awk's `print` command
    2. Just compare it

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Edinburgh, Scotland
    Posts
    1
    Or, you could try the sledgehammer approach, ignoring command line utilities and run up a little ruby script like this:

    # Usage: ruby compar.rb file2 file1
    # Build crossindex from file given as first argument
    keys = { }
    keyfile = File.new( "#{ARGV[0]}", 'r' )
    # Skip header section
    while !(keyfile.gets =~ /Code/) do end
    while line = keyfile.gets do
    row = line.split( "|" )
    keys[row[1]] = row[0] if row[1] != ""
    end
    keyfile.close

    # Make backup of target file given as second argument
    filename = ARGV[1]
    system( "cp #{filename} #{filename}.bak" )
    file = File.new( filename, 'r' )
    # Create temporary file
    newfile = File.new( "#{filename}.tmp", 'w' )
    # Copy header section
    while line = file.gets do
    newfile.puts line
    break if line =~ /Code:/
    end
    # Process each line
    while line = file.gets do
    row = line.split( "|" )
    4.upto( 6 ) { |i| row[i] = keys[row[i].strip] }
    buffer = ""
    0.upto( 7 ) { |i| buffer += (row[i] + (i < 7 ? '|' : "" )) }
    newfile.puts buffer
    end
    file.close
    newfile.close
    # Overwrite original file
    system( "mv #{filename}.tmp #{filename}" )

  4. #4
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Isn't it the same if you search for each entry as $2 (file2) in file1 and replace it with $1 (file2) ... as long as the other values won't contain any color.

    I am not sure how it will behave with lines like that:
    1237897||iuoy|iout|iufgt|90879
    There's the possibility that it will replace all "iuoy" with "1237897"

    Code:
    while read id color trash
    do
     cp file1.txt temp.txt
     sed "s/$color/$id/g" temp.txt > file1.txt
    done < file2.txt
    Should do it.

    Or you simply add it into a database and use a select command.

Posting Permissions

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