Home > front end >  Is it possible to put a pipeline in a variable
Is it possible to put a pipeline in a variable

Time:11-24

I have a long pipeline that I'm constantly reusing in my script, and to make it easy to read I want to put the pipeline in a variable. Is it possible?

cat miami.tmp | grep -A5 "$date" | grep -A3 "$nexthour" | grep "celsius" | grep -E -o  '[-]?[0-9].[0-9]' | head -n 1 >> miami.txt

I have tried

temperature=$( | grep -A5 "$date" | grep -A3 "$nexthour" | grep "celsius" | grep -E - 
o  '[-]?[0-9].[0-9]' | head -n 1 )

or

temperature="| grep -A5 "$date" | grep -A3 "$nexthour" | grep "celsius" | grep -E -o  '[-]?[0-9].[0-9]' | head -n 1"

but get errors saying the commands weren't found.

CodePudding user response:

You should put it in a function.

temperature ()  {
    grep -A5 "$date" |
    grep -A3 "$nexthour" |
    grep "celsius" |
    grep -E -o  '[-]?[0-9].[0-9]' |
    head -n 1
}

Maybe you want to make the date and the hour into parameters.

temperature ()  {
    grep -A5 "$1" |
    grep -A3 "$2" |
    grep "celsius" |
    grep -E -o  '[-]?[0-9].[0-9]' |
    head -n 1
}

Separately, this looks like it desperately wants to be refactored to Awk.

temperature () {
    awk -v date="$1" nexthour="$2" '
       $0 ~ date { p=5 }
       p && p-- && ($0 ~ nexthour) { p=3 }
       p && p-- && /celsius/ { n = split($0, a, /[^-.0-9]/, a);
           for(i=1; i<=n;   i) if (a[i] ~ /^-?[0-9]\.[0-9]$/)
               { print(a[i]); exit }'
}

(Untested, as you don't supply test data. I had to guess some things. If you are calling it by systematically looping over dates and hours, probably refactor that into the Awk script, too.)

Usage:

temperature 2022-11-24 04 <miami.tmp >>miami.txt

Probably see also https://mywiki.wooledge.org/BashFAQ/050

CodePudding user response:

This is a good case for using bash's shell functions. You can define a function like this:

function temperature() { grep foo | grep bar | grep baz; }

just make sure that the last command ends with a semicolon. You call the function with

cat file.tmp | temperature

Functions can also have parameters, accessed with the usual $1, $2 etc. notation, that can be passed in (space-separated) to the function.

$ function hello() { echo "Hello $1!"; }
$ hello world
Hello world!
  • Related