Bob
Bob

Reputation: 33

Merge two .txt files together and reorder

I have two .txt files both 42 lines each (42nd line is just a blank space).

One file is called date.txt, and each line has a format like:

2017-03-16 10:45:32.175 UTC

The second file is called version, and each line has a format like:

1.2.3.10

Is there a way to merge the two files together so that the date is appended to the version number (separated by a space). So the 1st line of each file is merged together, then the second line, third line, etc...

So it would look like:

1.2.3.10 2017-03-16 10:45:32.175 UTC

After that, is it possible to reorder the new file by the date and time? (Going from the oldest date to the latest/current one).

The end file should still be 42 lines long.

Thanks!

Upvotes: 0

Views: 37

Answers (1)

Reut Sharabani
Reut Sharabani

Reputation: 31339

Use paste:

paste file1.txt file2.txt -d' ' > result.txt

-d is used to set the delimiter.

You can then attempt to sort by second and third column using sort:

sort -k2,3 result.txt > sorted.txt

-k is used to select columns to sort by.

But note that this doesn't parse the date and time, only sorts them as strings.

In general:

paste file1.txt file2.txt | sort -k2,3 > result.txt

Upvotes: 1

Related Questions