LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Quick way to convert spaces in text file to tabs to copy to spreadsheet? (https://www.linuxquestions.org/questions/linux-general-1/quick-way-to-convert-spaces-in-text-file-to-tabs-to-copy-to-spreadsheet-4175659229/)

linustalman 08-16-2019 04:11 AM

Quick way to convert spaces in text file to tabs to copy to spreadsheet?
 
Hi.

I have a file with dates and then weights (kg). They are separated by a space.

i.e.:
2019-08-01 68.7

I'd like to copy this long list to a spreadsheet once in a while to show a graph of my weight and other peoples weights. Is there a quick method to do this?

Thanks.

syg00 08-16-2019 04:15 AM

tr, sed, awk, perl, python, ...

Start with the manpage for tr.

pan64 08-16-2019 04:24 AM

Quote:

Originally Posted by linustalman (Post 6025676)
Is there a quick method to do this?

The answer is: yes. You can also implement it easily in bash.
But from your side: Which language/tool do you prefer? What did you try so far? Where did you stuck?

EmaRsk 08-16-2019 06:38 AM

Code:

tr " " "\t" <original_file >converted_file
or
Code:

sed "s/ /\t/" original_file >converted_file

michaelk 08-16-2019 06:55 AM

Depends on spreadsheet app. Excel and libreoffice as far as I know have the capability to select the delimiter i.e as a space, tab or comma etc or fixed width so conversion is probably not necessary.

syg00 08-16-2019 07:14 AM

So if the OP had made some effort .... :shrug:

rtmistler 08-16-2019 08:01 AM

Quote:

Originally Posted by michaelk (Post 6025711)
Depends on spreadsheet app. Excel and libreoffice as far as I know have the capability to select the delimiter i.e as a space, tab or comma etc or fixed width so conversion is probably not necessary.

Quote:

Originally Posted by syg00 (Post 6025718)
So if the OP had made some effort .... :shrug:

Agree and concur.

Not so proficient with the Linux varieties of spreadsheets, but I know that one can import data and many times there's a preview of that data which shows, and allows you to change you delimiter so that you can get it right, while viewing the previews.

So just try this, and I would prefer to stay away from a bunch of text processing tools. I do not feel they are necessary.

Or, to be kinder about it. Have you already tried this, and found it to be flawed in some manner?

mjolnir 08-16-2019 09:12 AM

Calc will import the file fine checking 'space' as a delimiter.
Off Topic: I understand the rationale for asking someone who just joined what they have tried but IMO grilling a Contributing Member who has 3500+ posts is unnecessary. Just my 2 cents.

linustalman 08-16-2019 09:45 AM

Quote:

Originally Posted by EmaRsk (Post 6025707)
Code:

cat original_file | tr " " "\t" >converted_file
or
Code:

sed "s/ /\t/" original_file >converted_file

Hi EmaRsk. Thank you very much. Those are perfect!

linustalman 08-16-2019 09:46 AM

Quote:

Originally Posted by pan64 (Post 6025679)
The answer is: yes. You can also implement it easily in bash.
But from your side: Which language/tool do you prefer? What did you try so far? Where did you stuck?

Hi pan64. I'm not too fussed on the language. It's solved now anyway. Thanks.

linustalman 08-16-2019 09:48 AM

1 Attachment(s)
Quote:

Originally Posted by mjolnir (Post 6025769)
Calc will import the file fine checking 'space' as a delimiter.
Off Topic: I understand the rationale for asking someone who just joined what they have tried but IMO grilling a Contributing Member who has 3500+ posts is unnecessary. Just my 2 cents.

Hi mjolnir. Yes, that's ideal. Thank you.

jmccue 08-16-2019 03:19 PM

I would use unexpand(1). It should already be on your distro :)

pan64 08-17-2019 01:29 AM

hm. I have never heard about expand/unexpand, both can be implemented easily in awk/perl/python/whatever, still interesting that we have a ready-made tool for this.
something to learn again.

linustalman 08-17-2019 11:18 AM

Quote:

Originally Posted by syg00 (Post 6025678)
tr, sed, awk, perl, python, ...

Start with the manpage for tr.

So basically a slightly helpful rtfm then, right?

linustalman 08-17-2019 11:18 AM

Quote:

Originally Posted by syg00 (Post 6025718)
So if the OP had made some effort .... :shrug:

Not very helpful at all.


All times are GMT -5. The time now is 09:23 PM.