I need to fetch the data between certain limits. In the above example if we pass the project3,project4 as arguments, I need the contents between those 2 arguments.
In between project3 & project4 below contents are there
$0 ~ P1 { # Check if current record matches pattern: P1 (project.*3)
f = 1 # If yes, then set flag variable f = 1
next # Skip processing current record
}
$0 ~ P2 { # Check if current record matches pattern: P2 (project.*4)
f = 0 # If yes, then set flag variable f = 0
next # Skip processing current record
}
f # If f == 1 (true) AWK's default operation is to print current record
Basically we are setting a flag variable f to 1 when first pattern is matched & we set it to 0 when second is matched, the records are printed only if the flag is set to 1.
The code hanson44 provided didn't notice that the original input had project3 and project 4 with no indication of whether or not searching for lines in other cases will or will not have one or more spaces in the middle of the project name.
The code Yoda provided assumes that there won't be multiple matching project names. (For example, if projects 1 through 40 are in your input file with or without spaces in their names, Yoda's code will print data between the lines starting project3 and project4, project 13 and project 14, project 23 and project 24, and project 30 and project 34.)
You might want to try something like this instead:
It follows the logic used by Yoda, but searches for lines identifying projects based on matching the entire line against "project" followed by zero or more spaces, followed by the project number occurring at the end of an input line. It exits when it finds the ending project instead of reading the rest of the input file.