如何在匹配模式后打印下一行,然后在使用awk再次出现匹配模式时停止打印?
我有一个SQL文件,格式如下:如何在匹配模式后打印下一行,然后在使用awk再次出现匹配模式时停止打印?,awk,Awk,我有一个SQL文件,格式如下: -- 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, am
-- 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;
仅当问题编号作为编号提供给PROB
变量时,我才试图打印出包含查询的行,然后在遇到另一个问题时停止
例如,我希望第二个问题的输出如下:
$ 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;
$ 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;
或者,第一个问题的输出如下:
$ 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;
$ 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;
我尝试了以下操作,但得到的输出不是我想要的:
/^--/{
RS="-- "
getline
}
$0 ~ PROB{
print $0
}
如何修改我的awk
脚本以获得所需的输出?编辑:添加代码以删除我以前代码中的控件M字符,因为OP的输入文件中有控件M字符
awk -v PROB=1 '{gsub(/\r/,"")} /^-/{next} !/^ +/&&!/^$/{count++} count==PROB && NF' Input_file
请您尝试以下内容,并让我知道这是否有助于您
awk -v PROB=2 '/^-/{next} !/^ +/&&!/^$/{count++} count==PROB && NF' Input_file
如果您给出PROB=1
,则以下为输出
awk -v PROB=1 '/^-/{next} !/^ +/&&!/^$/{count++} count==PROB && NF' Input_file
说明:
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.
请在您的帖子中的代码标签中发布预期输出。问题中提供了预期输出。我希望它输出每个问题的查询。因此,对于第一个问题,我会这样做:
$awk-f get\u query.awk-v PROB=1 querys.sql
只获取sql查询。这意味着您不需要以-
等开头的行?是的,对于awk声明中给定给PROB
的特定问题编号,我试图仅获取不以-
开头的行。请检查我的答案,并让我知道这是否有帮助?现在删除上述代码的输出,只保留其中的代码和解释。当我尝试在具有awk版本的机器上运行此操作时3.1.7我没有得到我想要的输出。有可能使它与那个版本兼容吗?@Grey,即使是我的版本也太GNU Awk 3.1.7
了,而且对我来说工作得很好。这真的很奇怪。当我试图运行它时,我得到一个空行作为输出。@Grey,也检查cat-v输入\ u文件
是否在其中看到任何控件M字符?如果是,请务必在同一时间通知我。