[SOLVED] Script command for make a table

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
mmittelbach
Level 1
Level 1
Posts: 45
Joined: Fri Jul 10, 2020 7:09 pm

[SOLVED] Script command for make a table

Post by mmittelbach »

Good morning.
I need to format a kind of economic data.
I build some bin bash, then I recive csv "dirty" data and I like to sort this data in columns, but I can't find the tool.
I send some image, I like to build something like this: (forgiveme but my english is too horrible).
Thank you very much.
Matias Mittelbach
Attachments
sortdata.png
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
t42
Level 11
Level 11
Posts: 3734
Joined: Mon Jan 20, 2014 6:48 pm

Re: Scritp command for make a table

Post by t42 »

For numeric sort per column 1 and ',' delimiter

Code: Select all

sort -k1 -n -t ',' filename
sort -k1 -n -t ',' filename >filename_sorted
It's hard to say more as there is no sample data and picture is too small

Code: Select all

man sort
-=t42=-
mmittelbach
Level 1
Level 1
Posts: 45
Joined: Fri Jul 10, 2020 7:09 pm

Re: Script command for make a table

Post by mmittelbach »

Thank you very much.
I will prove
This is an image of the csv data.
Thanks
Matias
Attachments
data.png
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script command for make a table

Post by rene »

Also only a quick reply due to data not having been supplied as simple text (attached, or simply pasted into the post, preferably within a [code]...[/code] block) but be sure to note the column utility. I.e.,

Code: Select all

rene@hp8k:~$ cat foo.csv 
a,bb,ccc,dddd,eee,ff,g
1,2,3,4,5,6,7
iiii,iii,ii,i,ii,iii,iiii
rene@hp8k:~$ column -ts, foo.csv 
a     bb   ccc  dddd  eee  ff   g
1     2    3    4     5    6    7
iiii  iii  ii   i     ii   iii  iiii
mmittelbach
Level 1
Level 1
Posts: 45
Joined: Fri Jul 10, 2020 7:09 pm

Re: Script command for make a table

Post by mmittelbach »

I recieved the original data in a libreoffice calc, like this:
Then I need to build the script to view this data via terminal
Attachments
originaldata.png
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script command for make a table

Post by rene »

Yes. So does column -ts, infomondia.csv provide you with something usable for your purposes? man column for adjustment options.
mmittelbach
Level 1
Level 1
Posts: 45
Joined: Fri Jul 10, 2020 7:09 pm

Re: Script command for make a table

Post by mmittelbach »

I made this:
The problem is the second column (he is the "no natural" or fraction part of the first column p.e: 3347383,10=first column. Now the first column is 3347383 and the second column is 10). I need to resolve this ...
Attachments
firstcolumn.png
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script command for make a table

Post by rene »

Quoted fields containing comma's. Yah. Sucks.

The proper answer at this point is pointing you to e.g. sudo apt-get install csvkit and its documentation but in a "couldn't help myself" sense: it's very unhelpful that you are still not providing (a sampling of) your data as copy-pastable text rather than as screenshots so I tested this only on your first line:

Code: Select all

rene@p55m:~$ cat bcramon.csv 
Adelantos y documentos,"$1.455.152,90",$1.424.806,"2,4","2,1","18,6","23,5"
rene@p55m:~$ sed -e 's/"\([^,"]\+\),\([^"]\+\)"/\1|\2/g' -e 's/,/@/g' -e 's/|/,/g' bcramon.csv | column -ts@
Adelantos y documentos  $1.455.152,90  $1.424.806  2,4  2,1  18,6  23,5
What it does is:

1. Turn quoted fields containing a , into unquoted ones with said , replaced by |
2. Replace all occurrences of , with @, i.e., turning comma separated values into @-separated ones.
3. Replace the in step 1 introduced | with , again
4. Feed the result to column while with -s@ telling it to now consider the input @-separated.

It's of course important that your data contains neither | nor @ legitimately; any other pair of symbols can be used (or for the former even e.g. the word COMMA) if it does. As already said, only tested on one line due to not wanting to type it all in myself. And as finally also already said, you're in fact much better off with e.g. csvkit, but, well, ...
mmittelbach
Level 1
Level 1
Posts: 45
Joined: Fri Jul 10, 2020 7:09 pm

Re: Script command for make a table

Post by mmittelbach »

Thanks, I would try like this, then I will tell you.
mmittelbach
Level 1
Level 1
Posts: 45
Joined: Fri Jul 10, 2020 7:09 pm

Re: [SOLVED] Script command for make a table

Post by mmittelbach »

Hi rene, in order to a script:
sed -e 's/"\([^,"]\+\),\([^"]\+\)"/\1|\2/g' -e 's/,/@/g' -e 's/|/,/g' ravalider_loffice.csv | column -ts@

works perfectly!
lok at this example
Now i need to automate the circuit:
Dowonload de html page (turn to tmp), convert to csv, with libreoffice calc, make the script to view the data via terminal and make any loop to refresh data.
Thank you so much!
Attachments
script.png
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Script command for make a table

Post by rene »

Thanks for reporting; good to know it was useful.
Locked

Return to “Scripts & Bash”