Andrés Sanz
Andrés Sanz

Reputation: 55

Format grep output as CSV

I'm not really good at Linux. I am using the next command to find some data:

svn info -R https://SOME_URL/TEST | grep 'Ruta: \|Fecha de último cambio: '

The output I'm getting looks like this:

Ruta: TEST
Fecha de último cambio: 2016-04-07 15:52:40 -0500 (jue 07 de abr de 2016)
Ruta: PRUEBA1.txt
Fecha de último cambio: 2016-04-07 15:16:19 -0500 (jue 07 de abr de 2016)
Ruta: PRUEBA2.txt
Fecha de último cambio: 2016-04-07 15:15:47 -0500 (jue 07 de abr de 2016)

But I need to present a report so I would like to see the output as CSV document, something like:

"Ruta";"Fecha"
"TEST";"2016-04-07 15:52:40 -0500 (jue 07 de abr de 2016)"
"PRUEBA1.txt";"2016-04-07 15:16:19 -0500 (jue 07 de abr de 2016)"
"PRUEBA2.txt";"2016-04-07 15:15:47 -0500 (jue 07 de abr de 2016)"

Is it possible to do that only with linux commands? Thanks!

Upvotes: 3

Views: 3294

Answers (3)

karakfa
karakfa

Reputation: 67497

another awk alternative

$ awk -v q='"' 'NR==1{print q "Ruta" q ";" q "Fecha" q} 
             /^Ruta:/{t=q $2 q; next}
                     {sub(/[^:]+: /,"");
                      print t ";" q $0 q}' file 

"Ruta";"Fecha"
"TEST";"2016-04-07 15:52:40 -0500 (jue 07 de abr de 2016)"
"PRUEBA1.txt";"2016-04-07 15:16:19 -0500 (jue 07 de abr de 2016)"
"PRUEBA2.txt";"2016-04-07 15:15:47 -0500 (jue 07 de abr de 2016)"

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203502

The right tool for this job is awk, and you don't need grep when you're using awk:

$ cat tst.awk
NR==1 { fmt="\"%s\";\"%s\"\n"; printf fmt, "Ruta", "Fecha" }
sub(/^Ruta: /,"") { ruta=$0 }
sub(/^Fecha de último cambio: /,"") { printf fmt, ruta, $0 }

$ svn info -R https://SOME_URL/TEST | awk -f tst.awk
"Ruta";"Fecha"
"TEST";"2016-04-07 15:52:40 -0500 (jue 07 de abr de 2016)"
"PRUEBA1.txt";"2016-04-07 15:16:19 -0500 (jue 07 de abr de 2016)"
"PRUEBA2.txt";"2016-04-07 15:15:47 -0500 (jue 07 de abr de 2016)"

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84561

There are quite a number of ways this can easily be done. You can either use your shell (any modern shell with parameter expansion and substring removal) or you can use sed or awk or any combination thereof.

You haven't specified the shell, but as long as you have a POSIX compliant shell, a short script can handle/parse the results of your svn command in a fairly easy manner. The following uses bash, but parameter expansion would work in any POSIX shell:

#!/bin/bash

fname="${1:-/dev/stdin}"  ## read from given filename or stdin (default)

echo "Ruta;Fecha"       ## print heading

while read -r line; do  ## for each line of input
    [ "${line%%:*}" = "Ruta" ] && echo -n "${line##* }" ## begins 'Ruta'
    [ "${line%%:*}" = "Fecha de último cambio" ] && {   ## begins "Fecha.."
        tmp="${line#*:}"
        echo ";${tmp:1}"
    }
done < "$fname"

Input File

$ cat dat/ruta.txt
Ruta: TEST
Fecha de último cambio: 2016-04-07 15:52:40 -0500 (jue 07 de abr de 2016)
Ruta: PRUEBA1.txt
Fecha de último cambio: 2016-04-07 15:16:19 -0500 (jue 07 de abr de 2016)
Ruta: PRUEBA2.txt
Fecha de último cambio: 2016-04-07 15:15:47 -0500 (jue 07 de abr de 2016)

Use/Output

$ cat dat/ruta.txt | bash parseruta.sh
Ruta;Fecha
TEST;2016-04-07 15:52:40 -0500 (jue 07 de abr de 2016)
PRUEBA1.txt;2016-04-07 15:16:19 -0500 (jue 07 de abr de 2016)
PRUEBA2.txt;2016-04-07 15:15:47 -0500 (jue 07 de abr de 2016)

To use with your command (after you make the script executable with chmod 0755 scriptname) you could do the following:

svn info -R https://SOME_URL/TEST | \
grep 'Ruta: \|Fecha de último cambio: ' | \
scriptname

(that is all one line above with the line continuations '\' at the end)

Give it a try and let me know if you have questions.

If you are limited to a POSIX shell (or shell without string indexing in the form ${var:start:end}), then there is one command that would need to change. POSIX handles string indexing in a different manner, so you would need to change:

echo ";${tmp:1}"

to

echo $(expr substr "$tmp" 2 $(expr length "$tmp"))

in order to index the substring beginning with the date from the 2nd character after removing the "Fecha..." part.

Upvotes: 2

Related Questions