Skip to content
Advertisement

Ubuntu bash script grepping and cutting on first column

I am trying to implement a bash script that will take the piped input and cut the first column and return a total. The difference is that the cut is not from a file, but from the piped input. For example:

#!/bin/bash
total=$(cut -f1 $1 | numsum)
echo $total

Basically, in my scenario the first column will always be from the passed input.

Example usuage is:

 ./script.sh "cat data.txt | grep -i status | grep something"

data.txt contains:

1 status
2 something

This will produce something like:

2

How can this be achieved? I have noticed the cut only works for files only. I cannot see any examples on Google.

Advertisement

Answer

I have managed to fix the issue myself. The code is:

#!/bin/bash
total=$(eval $1 | awk '{sum+=$1} END {print sum}')
echo $total
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement