Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Find and replace multiple values between two files


    Hi, I'm Matiasfrom Argentina. First of all, I'm sorry about my bad english. My problem is the next: I have two files, one is "Index" and the other is "Data", both of them .txt. They look like this:

    Index

    Name ID
    Juan 1234
    Pedro 5678
    Pablo 9123
    Josť 4567

    Data

    x ID
    x Juan
    x Juan
    x Josť
    x Josť
    x Pablo
    x Pablo
    x Pedro
    x Pedro

    I need to search from the file "Index" row n col 1 on the file "Data" col 2 and replace the name with the ID that is in col 2 of "Index".
    It's like a vlook on excel but I need to search from the matrix to the data set.
    I cannot use the excel because "Data" have 5.040.000 lines. Each name of "Index" is repeated 9000 times in "Data".

    Thank you very much,
    Matias.

  2. #2
    Quote Originally Posted by matias_bermann View Post
    Hi, I'm Matiasfrom Argentina. First of all, I'm sorry about my bad english. My problem is the next: I have two files, one is "Index" and the other is "Data", both of them .txt. They look like this:

    Index

    Name ID
    Juan 1234
    Pedro 5678
    Pablo 9123
    Josť 4567

    Data

    x ID
    x Juan
    x Juan
    x Josť
    x Josť
    x Pablo
    x Pablo
    x Pedro
    x Pedro

    I need to search from the file "Index" row n col 1 on the file "Data" col 2 and replace the name with the ID that is in col 2 of "Index". It's like a vlook on excel but I need to search from the matrix to the data set. I cannot use the excel because "Data" have 5.040.000 lines. Each name of "Index" is repeated 9000 times in "Data".
    so since you knwo what you want to do what have you actually tried to DO IT? we arent going to write your script for you. lots of bash tutorials you can find if you look.

    grep can search a file. sed can replace values. have at it.

  3. #3
    you can write a bash script that will first read both columns from the first file into separate variables, and will then use sed to replace each occurence of "Name" (col 2 in Data) with "ID" (col 2 in index).
    let us know how it goes, show us what you got.
    How to ask smart questions | Don't be a Help Vampire
    You can post a link by removing "http://www." from it!

  4. $spacer_open
    $spacer_close
  5. #4
    Thanks! I will try it. I try:
    sed s/$1"index"/$2"index"/g data > temp
    but it doesn't work. The command for each name separately works very well, but since I'm begginer in linux, I didn't know that was not the way to refer to the columns and to repeat the process.
    Thank you very much!

  6. #5
    that's not a full bash script.
    please find yourself some beginners' tutorials, learn, try to modify some existing script...

    or learn to formulate search terms that will help you find a readymade solution; i'm sure something very similar has been asked already somewhere.
    How to ask smart questions | Don't be a Help Vampire
    You can post a link by removing "http://www." from it!

Posting Permissions

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