0

I am new to the Topic and working on a few bash commands.

I am stuck at a task where i Need t ouput my result in a specific Format.

wc -l folder/*

The Output gives me:

0 folder/file1
3 folder/file2
3 total

I would like to have only the total output together with a String like:

The total number of lines is: numberOfLines 

How is it possible to format my output / reduce it to a single line? I looked a bit into awk commands but I was not able to make it work.

Thank you very much in advance!

1 Answer 1

1

The simplest way to get a grand total is to concatenate the files first, and pass the concatenation to wc.

Ex.

printf 'The total number of lines is: %d\n' "$(cat folder/* | wc -l)"

However if you want to use awk, here's how I'd do it:

wc -l folder/* | awk 'END{print "The total number of lines is: " $1}'
3
  • 1
    You probably meant wc -l folder/* | awk '$2 == "total" {print "..." $1}' Nov 8, 2020 at 16:39
  • @glennjackman oops thanks - I've gone with a slight variant Nov 8, 2020 at 16:54
  • or even awk 'END{ print "The total number of lines is: " NR}' files* Nov 8, 2020 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .