如何在匹配模式后打印下一行,然后在使用awk再次出现匹配模式时停止?

时间:2018-04-01 04:55:07

标签: 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, 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;

我尝试了以下内容,但我得到的输出并不是我想要的:

/^--/{
    RS="-- "
    getline
}
$0 ~ PROB{
    print $0
}

如何修改我的awk脚本以获得所需的输出?

2 个答案:

答案 0 :(得分:1)

编辑: 在我之前的代码中添加用于删除控制M字符的代码,因为OP在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.

答案 1 :(得分:0)

$ 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;