Reputation: 49
I have a SQL file in the following format:
-- 1. Create a view 'c_summary' summarizing campaign contributions,
-- with four attributes: cand_name, contbr_name, amount, and zip.
create view c_summary as
select candidate.name as cand_name, contributor.name as contbr_name, amount, zip
from candidate inner join contributor inner join contribution
on candidate.cand_id = contribution.cand_id and
contributor.contbr_id = contribution.contbr_id;
-- 2. For each of the occupations "STUDENT", "TEACHER", and "LAWYER",
-- show the occupation, and average size (in dollars) of contribution
-- from contributors with that occupation.
select occupation, round(avg(amount))
from contributor natural join contribution
where occupation in ("STUDENT", "TEACHER", "LAWYER")
group by occupation;
I am trying to print out the lines that contain the query only when a question number is provided as a number to the PROB
variable, and then to stop once another question is reached.
For instance, I want the output to be like this for the second question:
$ awk -f get_query.awk -v PROB=2 queries.sql
select occupation, round(avg(amount))
from contributor natural join contribution
where occupation in ("STUDENT", "TEACHER", "LAWYER")
group by occupation;
Or, the output to be like this for the 1st question:
$ awk -f get_query.awk -v PROB=1 queries.sql
create view c_summary as
select candidate.name as cand_name, contributor.name as contbr_name, amount, zip
from candidate inner join contributor inner join contribution
on candidate.cand_id = contribution.cand_id and
contributor.contbr_id = contribution.contbr_id;
I tried the following, but the output I am getting is not what I want to get:
/^--/{
RS="-- "
getline
}
$0 ~ PROB{
print $0
}
How would I modify my awk
script to get my desired output?
Upvotes: 2
Views: 90
Reputation: 133528
EDIT: Adding code for removing the control M characters too now in my previous code as OP has control M characters in OP's Input_file.
awk -v PROB=1 '{gsub(/\r/,"")} /^-/{next} !/^ +/&&!/^$/{count++} count==PROB && NF' Input_file
Could you please try following and let me know if this helps you.
awk -v PROB=2 '/^-/{next} !/^ +/&&!/^$/{count++} count==PROB && NF' Input_file
In case you give PROB=1
then following will be the output.
awk -v PROB=1 '/^-/{next} !/^ +/&&!/^$/{count++} count==PROB && NF' Input_file
Explanation:
awk -v PROB=2 ' ##Creating a variable named PROB whose value is 2 here.
/^-/ { next } ##Checking if a line starts from dash then using next it will skip all further statements.
!/^ +/ && !/^$/ { count++ } ##Checking if a line not starts from space and is NOT a NULL line then increase the value of variable named count to 1.
count==PROB && NF ##Checking condition here if variable count value is equal to variable PROB and line is NOT an empty line then print it.
' Input_file ##Mentioning the Input_file name here.
Upvotes: 1
Reputation: 203615
$ awk -v RS= -v p=2 '(NR/2)==p' file
select occupation, round(avg(amount))
from contributor natural join contribution
where occupation in ("STUDENT", "TEACHER", "LAWYER")
group by occupation;
$ awk -v RS= -v p=1 '(NR/2)==p' file
create view c_summary as
select candidate.name as cand_name, contributor.name as contbr_name, amount, zip
from candidate inner join contributor inner join contribution
on candidate.cand_id = contribution.cand_id and
contributor.contbr_id = contribution.contbr_id;
Upvotes: 0