formatting sql command export with sed

Asked

Viewed 64 times

0

I have exported a table from my database as follows:

"1";"Boi Preto";"2000-02-29";"2";"Sol Nascente";"2009-10-01";"3";"Parque Belo";"2007-03-15";"4";"Pedras Bonitas";"2017-12-12";"5";"Medeiros";"2011-06-22";

and needed to leave as follows below:

01; Boi Preto; 2000-02-29
02; Sol Nascente; 2009-10-01
03; Parque Belo; 2007-03-15
04; Pedra Bonita; 2001-08-25
05; Nossa Senhora; 2011-06-22

how do I do it with sed?

1 answer

1


With awk, prints three by three;

$ awk 'BEGIN{FS=OFS=";"} {for (i=1; i<NF; i+=3) print $i, $(i+1), $(i+2)}' arquivo
"1";"Boi Preto";"2000-02-29"
"2";"Sol Nascente";"2009-10-01"
"3";"Parque Belo";"2007-03-15"
"4";"Pedras Bonitas";"2017-12-12"
"5";"Medeiros";"2011-06-22"

If you want, you can save the code to a file:

$ cat mola.awk 
BEGIN{FS=OFS=";"}
{for (i=1; i<NF; i+=3) print $i, $(i+1), $(i+2)}

And facer:

$ awk -f mola.awk arquivo
"1";"Boi Preto";"2000-02-29"
"2";"Sol Nascente";"2009-10-01"
"3";"Parque Belo";"2007-03-15"
"4";"Pedras Bonitas";"2017-12-12"
"5";"Medeiros";"2011-06-22"
  • 1

    I created a file called fmt.awk to make it easier did chmod +x fmt.awk and cat esportacao.txt | . /fmt.awk

  • @dark777 O awk -f fmt.awk esportacao.txt :)

  • I did this but it returned the following error below: bash-4.4$ awk -f fmt.awk export.txt awk: fmt.awk:2: awk 'BEGIN{FS=OFS=";"} {for (i=1; i<NF; i+=3) print $i, $(i+1), $(i+2)}' | sed’s/"//g;s/$/;/g' awk: fmt.awk:2: invalid char ''' in Expression awk: fmt.awk:2: awk 'BEGIN{FS=OFS=";"} {for (i=1; i<NF; i+=3) print $i, $(i+1), $(i+2)}' | sed’s/"///g;s/$/;/g' awk: fmt.awk:2: syntax error removed sed it from the same error as sed would remove the quotes.

  • @dark777 I put the code in a file. Look at my edition

  • Now funfou mas não teria como usar o mesmo para remover as quotes?

  • @dark777 awk -f mola.awk arquivo | sed 's/"//g;s/$/;/g' funfou for me.

  • 1

    I did a search and managed to get it to remove the quotes: BEGIN{FS=OFS=";"} {gsub(""",""); for (i=1; i<NF; i+=3) print $i, $(i+1), $(i+2)}

Show 2 more comments

Browser other questions tagged

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