-1

this is the scenario. I have File1 and File2 and i like to have the outcome in File3. I'm kind of new to Linux, but so far ive tried to use sort, diff, and comm. but no luck so far.

File1.txt File2.txt > File3.txt

File1.txt

RB0009  8,89
RB0010  5,67
RB0015  4,32
RB0027  6,56

File2.txt

RB0009  8,89
RB0010  5,67
RB0015  4,32
RB0027  6,56
RB0033  9,78

File3.txt

RB0009  700111i 8,89
RB0010  700092i 5,67    
RB0015  700148i 4,32
RB0027  700123i 6,56

help would be much appreciated.

1
  • 9
    How are the values in the middle column of File3.txt generated? Commented Feb 14, 2023 at 13:02

1 Answer 1

0

Based on the comment below me, it seems like I misunderstood the question. This should give you the expected output:

paste File1.txt File2.txt | awk '{print $1"\t" "700" NR "i" "\t" $2}' > File3.txt

Misunderstanding below:


If I understand correctly, you have two input files (File1.txt and File2.txt) and you want to create an output file (File3.txt) that contains the sorted and combined content of the two input files?

Assuming that the first column of each file is a unique identifier (e.g., RB0009, RB0010, etc.), and that you want to combine the two files by matching these identifiers, you could use the join command in Linux.

Here's an example command that should accomplish what you're looking for:

join -t $'\t' -a 1 -a 2 -o auto <(sort File1.txt) <(sort File2.txt) > File3.txt

Breakdown of the command:

  • join is the command we're using to join the two files.

  • -t $'\t' tells join to use a tab (\t) as the field separator.

  • -a 1 -a 2 tells join to include all lines from both files, even if there are no matches.

  • -o auto tells join to output the fields from both files, separated by a tab.

  • <(sort File1.txt) and <(sort File2.txt) are process substitutions that sort the contents of each file before passing them to join. This is important because join requires sorted input files in order to work properly.

  • > redirects the output of the join command to File3.txt.

The resulting File3.txt should contain the combined content of the two input files, sorted by the first column (the unique identifier).

3
  • This does not give the output expected by the OP. It does not create the second column, only the first and third. Commented Feb 14, 2023 at 21:57
  • @doneal24 I've updated the answer. Commented Feb 14, 2023 at 22:11
  • The updated answer still does not work in my test. The middle column begins with 700 and ends with i but the number in between starts at 1 and increments in each row. How would you generate 700092i as the value on the second row? Commented Feb 15, 2023 at 18:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.