awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing.

one
two
three
four
five
ten
six
seven
eight

Expected output if I have passed one and ten

two
three
four
five

This one works for me when I pass value directly in awk.

cat in.txt | awk '/one/{flag=1;next}/ten/{flag=0}flag' 

With the expected below results

two
three
four
five

But when I use -v options it does not working for me, as I would like to pass the starting and ending point from the script.

start="one"
end="ten"
cat in.txt | awk -v v_start=$start -v v_end=$end '/v_start/{flag=1;next}/v_end/{flag=0}flag'
Did not work for me

Can some pls tell me what I am doing wrong

You need to use regex strings instead of regex constants. Try:

awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$~v_end{flag=0}flag'
1 Like

Hi Scrutinizer,
I think you dropped a character when copying your code here. Didn't you want $0 on both RE matches:

awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$0~v_end{flag=0}flag'
2 Likes

Thank you Scrutinizer and Don Cragun. It works great

Just noticed that I am not printing the delimiter which is start and end on my results.

From the sample file,

Sample file:

one
two
three
four
five
ten
six
seven
eight

If I am passing "one" and "ten" the expected result would be

start="one"
end="ten"
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$0~v_end{flag=0}flag'

Expected output would be:

one
two
three
four
five
ten

Can anyone pls help me with this code?

Hello mychbears,

Could you please try following and let me know if this helps you.

awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;}$0~v_end{print;flag=0}flag'  Input_file
OR
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;print;next}$0~v_end{print;flag=0}flag'  Input_file
OR
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;};flag;$0~v_end{flag=0}'  Input_file

Output will be as follows in above all 3 solutions.

one
two
three
four
five
ten

Thanks,
R. Singh

Try also

awk -vST=$start -vEN=$end '$0~ST, $0~EN' file
one
two
three
four
five
ten

You could also try:

sed -n "/$start/,/$end/p" file

But, note that all of the suggestions that have been given will also print all lines from a line that matches $start to the end of the file if no line after that matches the RE specified by $end . Your problem statement doesn't say what should happen in this case.

Thank you. I need to print all lines that matches start to end.

---------- Post updated at 09:23 PM ---------- Previous update was at 09:22 PM ----------

---------- Post updated at 09:23 PM ---------- Previous update was at 09:23 PM ----------

Thank you Singh. It worked great.

---------- Post updated at 09:24 PM ---------- Previous update was at 09:23 PM ----------

---------- Post updated at 09:25 PM ---------- Previous update was at 09:24 PM ----------

It worked great. Thank you all for your suggestions.