shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [shell-script] Pegar a data do arquivo e jogar diretamente no awk co


From: Ronaldo Ferreira de Lima
Subject: Re: [shell-script] Pegar a data do arquivo e jogar diretamente no awk como uma variável
Date: Tue, 2 Dec 2014 17:12:06 -0200
User-agent: mutt-ng/devel-r804 (Linux)

Saudações Cristiano,

On Tue, Dec 02, 2014 at 03:20:30PM -0200, Cristiano Amaral address@hidden 
[shell-script] wrote:
> Senhores tenho um arquivo com o seguinte conteúdo :
> 
> Nov 25 16:17:01 corp-bbb-exp open: user 11111111111 opened INBOX/Arquivo
> Remoto/Orcamento
> Nov 26 17:17:01 corp-bbb-exp open: user 22222222222 opened INBOX/Antigas
> Nov 27 18:17:01 corp-bbb-exp open: user 33333333333 opened INBOX/Arquivo
> Remoto/Pessoal
> 
> Estou tentando formatar a data como FT :
> 
> awk -v a=`date --date "Nov 25 16:17:01" +%FT%T` '{ print a, $7}' arquivo.txt
> 
> A pergunta é como faço para pegar a data do arquivo e jogar diretamente no awk
> como uma variável ?
Embora não tenha sido exatamente o que você perguntou, é bem simples
reformatar a data usando puro awk:

    #!/usr/bin/awk -f
    BEGIN {
        # Assumindo ano atual como referência:
        year = strftime("%Y", systime())
    
        # Criando um dicionário para os meses:
        for ( i = 1; i <= 12; i++ ) {
            time = mktime(year" "i" 01 00 00 00")
            mspec = strftime("%b\t%m", time)
            split(mspec, array, /\t/)
            mdict[array[1]] = array[2]
        }
    }
    {
        date      = year" "mdict[$1]" "$2
        time      = gensub(/:/, " ", "g", $3)
        string    = date" "time
        timestamp = mktime(string)
        fmtdate   = strftime("%FT%T", timestamp, 0)
    
        print fmtdate, $7;
    }

Realmente não  consigo enxergar vantagem alguma  em se usar shell  e awk
juntos neste caso. Para fazer em puro shell também é bem simples:

    while IFS=$' \n' read -a f; do  
        date=$(date -d"${f[0]} ${f[1]} ${f[2]}" +%FT%T); 
        echo $date ${f[6]};
    done 

[]'s
-- 
"Não manejo bem as palavras
Mas manipulo bem as strings."
------------------------------
http://tecnoveneno.blogspot.com


reply via email to

[Prev in Thread] Current Thread [Next in Thread]