Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a problem using awk. Print, from every file given as a parameter, the number of line that has the length at least 10. Also, print the content of that line(s), except the fist 10 characters. At the end of the analysis of a file print the name of the file and the number of lines printed.

This is what I've done so far:

{
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
}
}

This prints the name of the file and the number of lines after every line that has at least 10 characters but I want something like this:

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
share|improve this question

2 Answers 2

Try something like this:

#!/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"
}

If you run this on the files a, b and 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 
share|improve this answer

you can put your end-of-file summary in an END {...} block - it will get executed as the script exits.

i.e. get rid of the bogus x=wc -l and change the if (FNR > x) { ... } to just END { ... }

share|improve this answer
    
it's not working If I give 2 files as parameters –  Cucerzan Rares Mar 31 '13 at 11:16
    
yep, that's because an END block runs at the end of ALL input (or on exit), not at the end of every file. terdon's answer will work for multiple files. or you could use a for loop wrapper, or construct a list of filenames (with, e.g., find) and pipe into xargs -n 1 or any of several other methods of running the awk script once per file. –  cas Apr 2 '13 at 8:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.