Bash – How to pipe a command result to a – option? (Without spaces)


I have this set of piped commands:

grep -n '*' file.txt | head -n1 | awk -F\: '{print $1-1;}'

It tells me the previous line to that where it first find asterisks.
Now I want to get the previous lines piping that to:

head -n<that previous line number>

Head requires a number following immediately the -n argument, without spaces, for example:

head -n4

How can I do that? It just won't accept adding

| head -n

at the end of the set of commands.
My searches have been fruitless. Thanks!

Best Solution

You want back-ticks to substitute the value:

head -n`grep -n '*' file.txt | head -n1 | awk -F\: '{print $1-1;}'` file.txt

Or possibly something similar on multiple lines:

LINENO=`grep -n '*' file.txt | head -n1 | awk -F\: '{print $1-1;}'`
head -n${LINENO} file.txt