Format file . csv in bash

Asked

Viewed 126 times

0

I have the following problem: I copied the data from an Internet table into a text file. The goal is to turn this file to the default. csv in English (comma separated and decimal separator being the endpoint) plus some other formatting. Example of file:

Data    Fechamento  Variação    Variação (%)    Abertura    Máxima  Mínima  Volume
30 Abr 2020     2,00    0,76    61,29%  1,99    2,10    1,80    152.100
29 Abr 2020     1,24    -0,44   -26,19%     1,28    1,71    1,20    125.700

My code is like this:

echo -e "Arquivo nao-estruturado: \c"
read nome_arq

arq=$(<$nome_arq)

arq=$(echo $arq | sed 's/%//g')
arq=$(echo $arq | sed 's/()//g')
arq=$(echo $arq | sed 's/\.//g')
arq=$(echo $arq | sed 's/\+//g')
arq=$(echo $arq | sed 's/ Abr /_04_/g')
arq=$(echo $arq | sed 's/ Mar /\_03_/g')
arq=$(echo $arq | sed 's/\,/\./g')
arq=$(echo $arq | sed 's/\ /\,/g')

append="_clean"
echo -e $arq >> $nome_arq$append 

However, the script output is not line breaking, that is, the output file only contains a single line:

Data,Fechamento,Variação,Variação,Abertura,Máxima,Mínima,Volume,30_04_2020,2.00,0.76,61.29,1.99,2.10,1.80,152100,29_04_2020,1.24,-0.44,-26.19,1.28,1.71,1.20,125700,

Ideas for the original file lines to stay in the output file?

2 answers

0


I modified a little, making a copy of the file and changing the copy.

#!/bin/bash
echo -e "Arquivo nao-estruturado: \c"
read nome_arq

cp $nome_arq $nome_arq"_clean"
arq=$nome_arq"_clean"

sed -i 's/%//g;s/()//g;s/\.//g;s/\+//g;s/ Abr /_04_/g;s/ Mar /\_03_/g;s/\,/\./g' $arq
sed -r -i  's/[[:space:]]+/,/g' $arq

-2

This csv file may have been edited on Windows. And basically Linux "sees" the end of the line differently than Windows.

Try this:

sudo apt update
sudo apt install dos2unix
dos2unix arquivo.csv

Att.

  • Speak, brother. I managed csv on the same linux environment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.