Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I was trying to pass variable search strings inside AWK and use it as START and END criteria.
This is how it goes.
Within shell script
declare START and END asSTART_POINT="This is the Starting Point"
END_POINT="This is the End path"Pass the two variables to awk and to output all the lines falling under the two search strings.
i.e
awk -v START1="$START_POINT" -v END1="$END_POINT" '/START1/,/END1/ { print }' inputfilename.
My intention is to output all the lines in file "inputfilename" which are in between lines
This is the Starting Point
and
This is the End path
I cannot hardcode the lines inside awk programme as the start and end criteria has to be passed dynamically.Please help me out.

It is expecting RE's for the search arguments (between the slashes). For /START1/ it looks for string "START1".
But you can code your own:
awk -v START1=$x -v END1=$y '{
if (match($0,START1))
printflag=1
if (printflag==1)
if (match($0,END1))
printflag=0
}' infileThe above simulates the awk construct, printing possibly multiple groups of lines, including a group that "starts" and never "ends".
To process one group of lines only, you can include an exit when end string is detected.
To print each group of lines less the triggering "start" and "end" lines, just switch the first and third if-statements.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |