How to pass the result of the command to the parameter - <character> <argument>? (Without spaces)

I have this set of commands:

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

This tells me the previous line in which it first detects asterisks. Now I want to get the previous lines so that:

head -n<that previous line number>

The head requires a number immediately following the -n argument with no spaces, for example:

head -n4

How can i do this? He simply will not agree to add

| head -n

at the end of the instruction set. My searches were fruitless. Thanks!

+3
source share
3 answers

You want reverse ticks to replace the value:

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

Or perhaps something similar on several lines:

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

Why don't you just do:

awk -- '{if (!/*/) print $0; else exit}' file.txt

, :

awk -- '/*/ {exit}; {print}' file.txt
+2

, xargs. - :

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

Source: https://habr.com/ru/post/1708915/


All Articles