Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:05:17+00:00 2026-06-01T13:05:17+00:00

I have two CSV files which use @ to divide each column. The first

  • 0

I have two CSV files which use @ to divide each column. The first file (file1.csv) has two columns:

cat @ eats fish
spider @ eats insects

The second file (file2.csv) has four columns:

info @ cat @ info @ info
info @ spider @ info @ info
info @ rabbit @ info @ info

I need to add the information from the second column of the first file to a new column in the second file, in cases where the details of the first column of the first file and the second column of the second file match, e.g., the result of the above would make this:

info @ cat @ info @ info @ eats fish
info @ spider @ info @ info @ eats insects
info @ rabbit @ info @ info @

As seen above, as the first file contained no information about rabbits, a new empty column is added to the last row of the second file.

Here is what I know how to do so far:

while read line can be used to cycle through the rows in the second file, e.g.:

while read line
do
    (commands)
done < file2.csv

The data from particular columns can be accessed with awk -F "@*" '{print $n}', where n is the column number.

while read line
do
    columntwo=$(echo $line | awk -F "@*" '{print $2})
    while read line
    do
        columnone=$(echo $line | awk -F "@*" '{print $1})
        if [ “$columnone” == “$columntwo” ]
        then
            (commands)
        fi
    done < file1.csv
done < file2.csv

My approach seems inefficient and I am not sure how to use add the data from the second column of file1.csv1 to a new column in file2.csv.

  • Items in column 1 of file1.csv1 and column 2 of file2.csv are unique to those files. There are no duplicate entries within those files.
  • The resulting file should have exactly 5 columns in every line, even if some columns are empty.
  • The file contains a lot of characters from various languages in UTF-8.
  • There is white space around @, but if this causes problems with the script, I can delete this.

How can the data from the first file be added to the data in the second file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:05:18+00:00Added an answer on June 1, 2026 at 1:05 pm

    And a nice, clean awk solution:

    awk -F" *@ *" 'NR==FNR{lines[$2]=$0} NR!=FNR{if(lines[$1])lines[$1]=lines[$1] " @ " $2} END{for(line in lines)print lines[line]}' file2.csv file1.csv
    

    A nice one-liner. Not a short one, but not the longest I’ve seen. Note that file2 and file1 are switched. Again, as a script with explanation:

    #!/usr/bin/awk -f
    
    # Split fields on @ and the whitespace on either side.
    BEGIN { FS = " *@ *" }
    
    # First file
    NR == FNR {
        #Store the line
        lines[$2] = $0
    }
    
    # Second file
    NR != FNR {
        # If the appropriate animal was in the first file, append its eating habits.
        # If not, it's discarded; if you want something else, let me know.
        if(lines[$1]) lines[$1] = lines[$1] " @ " $2
    }
    
    # After both files have been processed
    END {
        # Loop over all lines in the first file and print them, possibly updated with eating habits.
        # No guarantees on order.
        for(line in lines) print lines[line]
    }
    

    Call as awk -f join.awk file2.csv file1.csv, or make executable and ./join.awk file2.csv file1.csv.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file which has two columns, a numeric ID ( IDVAR
I have two csv file. First File has date offerid clicks orders Second File
I have two CSV files (three columns) which I need to compare and extract
I have a CSV file with two columns: cat @ c a t dog
I have two .csv files containing correlation matrices exported from R. One file contains
I have the following PHP script which reads from two CSV files, At the
I have n csv files which I need to compare against each other and
I have two DataTables, A and B , produced from CSV files. I need
I have a list of csv files ( file1, file2, ... ) that have
I need to develop an application where two csv files are compared. The first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.