Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing math on a Bash function and declaring it into a variable in 1 line

Tags:

bash

I'd like to do a file rowcount and subtract one from it. I know how to do the first part of getting the rowcount with this function:

zcat filename$today.csv.gz | wc -l

But how do I subtract one from that value to account for headers and store it in a variable? I've tried

$(( zcat filename$today.csv.gz | wc -l - 1 ))

but that isn't working.

Do I have to store the first function's output as a variable first? Is that the recommended practice?

like image 981
simplycoding Avatar asked Nov 03 '15 22:11

simplycoding


1 Answers

This one-liner can do the job:

n=$(( $(zcat filename$today.csv.gz  | wc -l) - 1 ))
like image 85
anubhava Avatar answered Oct 13 '22 14:10

anubhava