Problema usando o awk

1

Eu tenho um problema usando o awk. Imprima, a partir de cada arquivo dado como parâmetro, o número da linha que tem o comprimento de pelo menos 10. Além disso, imprima o conteúdo dessa (s) linha (s), exceto os primeiros 10 caracteres. No final da análise de um arquivo, imprima o nome do arquivo e o número de linhas impressas.

Isso foi o que eu fiz até agora:

{
if(length($0)>10)
{
 print "The number of line is:" FNR
 print "The content of the line is:" substr($0,10)
 s=s+1
}
x= wc -l //number of lines of file
if(FNR > x) 
{
 print "This was the analysis of the file:" FILENAME
 print "The number of lines with characters >10 are:" s
}
}

Isso imprime o nome do arquivo e o número de linhas após cada linha que tem pelo menos 10 caracteres, mas eu quero algo assim:

print "The number of line is:" 1
print "The content of the line is:" dkhflaksfdas
print "The number of line is:" 3
print "The content of the line is:" asdfdassaf
print "This was the analysis of the file:" awk.txt
print "The number of lines with characters >10 are:" 2
    
por Cucerzan Rares 31.03.2013 / 12:26

2 respostas

1

Tente algo assim:

#!/usr/bin/gawk -f
{
    ## Every time we change file, print the data for
    ## the last file read (ARGV[ARGIND-1])
    if(FNR==1 && ARGIND>1){
        print "This was the analysis of file:" ARGV[ARGIND-1]
        print "The number of lines with >10 characters is:" s,"\n"
        s=0;
    } 
    if(length($0)>10){
        print "The line number is:" FNR
        print "The content of the line is:" substr($0,10)
        s=s+1   
    }
}
## print the data collected on the last file in the list
END{
    print "This was the analysis of file:" ARGV[ARGIND]
    print "The number of lines with >10 characters is:" s,"\n"
}

Se você executar isso nos arquivos a , b e c :

$ ./foo.awk a b c
The line number is:2
The content of the line is:kldjahlskdjbasd
This was the analysis of the file:a
The number of lines with characters >10 is:1 

The line number is:2
The content of the line is:ldjbfskldfbskldjfbsdf
The line number is:3
The content of the line is:kfjbskldjfbskldjfbsdf
The line number is:4
The content of the line is:ldfbskldfbskldfbskldbfs
The line number is:5
The content of the line is:lsjdbfklsdjbfklsjdbfskljdbf
This was the analysis of the file:b
The number of lines with characters >10 is:4 

The line number is:1
The content of the line is: asdklfhakldhflaksdhfa
This was the analysis of the file:c
The number of lines with characters >10 is:1 
    
por 31.03.2013 / 15:07
0

você pode colocar seu resumo de final de arquivo em um bloco END {...} - ele será executado quando o script sair.

i.e. livrar-se do falso x=wc -l e alterar o if (FNR > x) { ... } para apenas END { ... }

    
por 31.03.2013 / 12:57

Tags