ca_san
ca_san

Reputation: 193

How to count lines in a file and add an arbitrary number to the result?

I have a file that contains 'x' lines in it.

I need to display the number of lines in such file and add 'y'.

I know that wc -l does the trick and displays 'x' as the output, how can it be so that the output would be 'x+y'?

Upvotes: 1

Views: 69

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

You could do like this,

$ wc -l file
13 yi
$ y=12
$ wc -l file | awk -v var=$y '{print $1+var}'
25

Upvotes: 2

jim mcnamara
jim mcnamara

Reputation: 16379

You cannot change what wc -l gives, but you can write a function that does this example:

# with variables to match your x y example:
mylines()
{
   x=$(cat $1 | wc -l)  # this cat is to avoid the filename in output
   y=$2
   echo $(( $x + $y ))
}

Example usage: mylines somefile 19

will add 19 to the number of lines in myfile and display the sum

Upvotes: 2

Related Questions