How to copy paste as raw csv from formatted csv file using LibreOffice Calc?

1

Given a raw csv file:

$ cat foo.csv 
foo, bar
buzz, "quoted string"

Libre office calc text import with separator and text delimiter set

If I open this one with Libre Office with these settings, I can view it nicely. Yet if I select the entire document again and copy it, the clipboard contains:

foo  bar
buzz    quoted string

I was expecting the clipboard to restore the raw csv again.


The example is very contrived, yet background is that I want to only export a sub fraction from a larger csv without having to cherry-pick via sed or using

head -n 1 > example.csv
cat large.csv | grep SOME_CODE >> example.csv // sometimes the filter is not as easy

I like to use LibreOffice Calc as it offers a nice filter feature through Data > Autofilter making it easy to find the lines I want to use. Yet extracting that selection is not as straightforward.

I will use the smaller file for setting up a unit test, which is why I don't want to change the format of the csv file even if it most likely wouldn't make much of a different given a proper csv parser.

As the point of the unit test is to make sure it parses the actual format properly, and I don't want to test against LibreOffice's understanding of a CSV file.

How can I maintain the raw formatting when copying from a CSV file in LibreOffice Calc?

k0pernikus

Posted 2018-01-29T10:33:17.137

Reputation: 1 140

Answers

1

Instead of Autofilter, use Data -> More Filters -> Standard Filter and copy results to Sheet2.

standard filter

Then from Sheet2, go to File -> Save As with type Text CSV to produce the following file.

foo,bar

This is slightly different formatting from the original. If it needs to be exactly the same, then LO Calc is not the right tool. Use sed or similar.

Jim K

Posted 2018-01-29T10:33:17.137

Reputation: 2 601