awk打印带有n列的行并忽略最后一行

时间:2017-10-06 05:59:03

标签: bash awk

我试图在awk上做得更好并将其用于csv文件的某些格式化,该文件具有多个标题行,实际报告在n行之后开始。该报告还有一个需要丢弃的页脚。以下是我尝试将条件结合起来制作一份干净的报告

  awk -F, '{NF==25} END {NR>2 {print l} {l=$0}}' 6999_RACV_Master_Conversion_Report_csv_20171005_114903_618978908.csv > 6999_RACV_Master_Conversion_Report_csv_20171005_114903_618978908_formatted.csv

上面我能够摆脱页脚,但标题存在。这是hte示例数据

    Date/Time Generated,10/5/17 11:46 AM
    Report Time Zone,Australia/Sydney
    Account ID,6999
    Date Range,9/1/17 - 9/30/17
    Floodlight Configuration,Equals:6017727
    Activity,Equals:RACV_INT_MMO_Consideration (4136140)
    Activity,Equals:RACV_INT_MMO_Sales (4135439)
    Activity,Equals:RACV_PARTNER_TMNFATravel_Consideration (4136141)
    Activity,Equals:RACV_PARTNER_TMNFATravel_Sales (4136142)
    Campaign,Equals:RACV - C&E Display FY17 - NEW - 16.11.17 (10581787)
    Campaign,Equals:RACV - Commercial Insurance Display FY17 (11180171)
    Campaign,Equals:RACV - EHA Display FY17 - NEW - 16.11.16 (10559465)
    Campaign,Equals:RACV - EHA Online Video FY17 - NEW - 16.11.16 (10604948)
    Campaign,Equals:RACV - EHA The Block 2017 - Digital (20185347)
    Campaign,Equals:RACV - ERA Display FY17 - NEW - 16.11.16 (10548416)
    Campaign,Equals:RACV - ERA Online Video FY17 - NEW - 16.11.16 (10554300)
    Campaign,Equals:RACV - Business Loans Digital FY17 - NEW - 16.11.16 (10647172)
    Campaign,Equals:RACV - Finance Caravan Loans Display FY17 - NEW - 16.11.16 (10580422)
    Campaign,Equals:RACV - Finance Display FY17 - NEW - 16.11.16 (10598350)
    Campaign,Equals:RACV - Home Security Social FY17 - NEW - 16.11.16 (10701389)
    Campaign,Equals:RACV - Mobile Tyres FY18 - NEW 01.07.17 (20089973)
    Campaign,Equals:RACV - Energy Comapre FY18 (Child) (20214029)
    Campaign,Equals:RACV Home Products & Services (20321908)
    Campaign,Equals:RACV - Personal Insurance Display FY17 - NEW - 16.11.16 (10605443)
    Campaign,Equals:RACV - Personal Insurance Online Video  FY17 - NEW - 16.11.16 (10730454)
    Campaign,Equals:RACV - Renters Insurance FY1718 (20192006)
    Campaign,Equals:RACV - Resorts Display FY17 - NEW - 16.11.16 (10512704)
    Campaign,Equals:RACV - Resorts Online Video FY17 - NEW - 16.11.16 (10591278)
    Campaign,Equals:RACV - Travel Insurance FY17 Display - NEW - 16.11.16 (10613550)
    MRC Accredited Metrics,None

    Report Fields
    Date,Activity,Activity ID,Campaign,Campaign ID,Site (DCM),Placement,Creative,Impression Count,IsPartnerName (string),OrderType (string),OrderProd (string),OrderName (string),OrderID (string),OrderRevenue (string),OrderPAX (string),OrderQ
    uantity (string),OrderStep (string),OrderDateFrom (string),OrderDateTill (string),Conversion URL,Click-through Conversions,View-through Conversions,Total Conversions,Total Revenue
    2017-09-01,RACV_INT_MMO_Consideration,4136140,RACV - Business Loans Digital FY17 - NEW - 16.11.16,10647172,Accuen Australia,Business Loans_OMDP_DBM_Custom Affinity_Leaderboard_728x90_Desktop_P25-54_Performance_,RAC18580-Car-Loans-Digital
    -banners-728-x-90,12,,,,Car-Insurance-quote-Step-1,,,,,Step-1,,,;src=6017727;type=racv-0;cat=racv_000;u7=;u8=;u9=;u10=;u11=;u12=;u13=Car-Insurance-quote-Step-1;u16=;u17=;u18=Step-1;u20=;u21=;dc_lat=;dc_rdid=;tag_for_child_directed_treatm
    ent=;ord=1256092675587.8044;~oref=https://mymembership.racv.com.au/wps/portal/mqte/!ut/p/b1/hY3JDoJAEES_iHQPmAGOIATDKg4izIXgEgRZggskfL3oHa1bJa9eAYeUSBSpLBMKCfA2H8oif5Zdm9efzmkmIlWZYxFUQrZGTY-IE7AAUSQzkM4ALkTDf3sbeFF3x_nqAPwL_3D5m665QDpj8
    qLTkyCCBFcZq5SudwfdnUwprsbxMTHVrkLlNZ2I7zq9Z-DVi9Rke453e13T7oLhmtDwWrkJVvEG7M0tJA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_2069SKG108QSC0ABT1KOSO00M3/act/id=0/p=backJspName=QFBuote_About_You/p=javax.portlet.action=backActionSubmit/36551187388
    3/-/,0.00,1.00,1,0.000000000
    2017-09-01,RACV_INT_MMO_Consideration,4136140,RACV - Business Loans Digital FY17 - NEW - 16.11.16,10647172,Accuen Australia,Business Loans_OMDP_DBM_Custom Affinity_Leaderboard_728x90_Desktop_P25-54_Performance_,RAC18580-Car-Loans-Digital
    -banners-728-x-90,12,,,,Car-Insurance-quote-Step-1,,,,,Step-1,,,;src=6017727;type=racv-0;cat=racv_000;u7=;u8=;u9=;u10=;u11=;u12=;u13=Car-Insurance-quote-Step-1;u16=;u17=;u18=Step-1;u20=;u21=;dc_lat=;dc_rdid=;tag_for_child_directed_treatm
    ent=;ord=1936578182591.1943;~oref=https://mymembership.racv.com.au/wps/portal/mqte,0.00,1.00,1,0.000000000
    2017-09-01,RACV_INT_MMO_Consideration,4136140,RACV - Business Loans Digital FY17 - NEW - 16.11.16,10647172,Accuen Australia,Business Loans_OMDP_DBM_Custom Affinity_Leaderboard_728x90_Desktop_P25-54_Performance_,RAC18580-Car-Loans-Digital
    -banners-728-x-90,12,,,,Car-Insurance-quote-Step-1,,,,,Step-1,,,;src=6017727;type=racv-0;cat=racv_000;u7=;u8=;u9=;u10=;u11=;u12=;u13=Car-Insurance-quote-Step-1;u16=;u17=;u18=Step-1;u20=;u21=;dc_lat=;dc_rdid=;tag_for_child_directed_treatm
    ent=;ord=5398916632833.284;~oref=https://mymembership.racv.com.au/wps/portal/mqte/!ut/p/b1/hY3JDoJAEES_iHQPmAGOIATDKg4izIXgEgRZggskfL3oHa1bJa9eAYeUSBSpLBMKCfA2H8oif5Zdm9efzmkmIlWZYxFUQrZGTY-IE7AAUSQzkM4ALkTDf3sbeFF3x_nqAPwL_3D5m665QDpj8q
    LTkyCCBFcZq5SudwfdnUwprsbxMTHVrkLlNZ2I7zq9Z-DVi9Rke453e13T7oLhmtDwWrkJVvEG7M0tJA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_2069SKG108QSC0ABT1KOSO00M3/act/id=0/p=backJspName=QFBuote_About_You/p=javax.portlet.action=backActionSubmit/365511963564
    /-/,0.00,1.00,1,0.000000000
Grand Total:,---,---,---,---,---,---,---,---,---,---,---,---,---,---,---,---,---,---,---,---,810.00,39373.00,40183,0.000000000

预期产出: -

Date,Activity,Activity ID,Campaign,Campaign ID,Site (DCM),Placement,Creative,Impression Count,IsPartnerName (string),OrderType (string),OrderProd (string),OrderName (string),OrderID (string),OrderRevenue (string),OrderPAX (string),OrderQ
uantity (string),OrderStep (string),OrderDateFrom (string),OrderDateTill (string),Conversion URL,Click-through Conversions,View-through Conversions,Total Conversions,Total Revenue
2017-09-01,RACV_INT_MMO_Consideration,4136140,RACV - Business Loans Digital FY17 - NEW - 16.11.16,10647172,Accuen Australia,Business Loans_OMDP_DBM_Custom Affinity_Leaderboard_728x90_Desktop_P25-54_Performance_,RAC18580-Car-Loans-Digital
-banners-728-x-90,12,,,,Car-Insurance-quote-Step-1,,,,,Step-1,,,;src=6017727;type=racv-0;cat=racv_000;u7=;u8=;u9=;u10=;u11=;u12=;u13=Car-Insurance-quote-Step-1;u16=;u17=;u18=Step-1;u20=;u21=;dc_lat=;dc_rdid=;tag_for_child_directed_treatm
ent=;ord=1256092675587.8044;~oref=https://mymembership.racv.com.au/wps/portal/mqte/!ut/p/b1/hY3JDoJAEES_iHQPmAGOIATDKg4izIXgEgRZggskfL3oHa1bJa9eAYeUSBSpLBMKCfA2H8oif5Zdm9efzmkmIlWZYxFUQrZGTY-IE7AAUSQzkM4ALkTDf3sbeFF3x_nqAPwL_3D5m665QDpj8
qLTkyCCBFcZq5SudwfdnUwprsbxMTHVrkLlNZ2I7zq9Z-DVi9Rke453e13T7oLhmtDwWrkJVvEG7M0tJA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_2069SKG108QSC0ABT1KOSO00M3/act/id=0/p=backJspName=QFBuote_About_You/p=javax.portlet.action=backActionSubmit/36551187388
3/-/,0.00,1.00,1,0.000000000
2017-09-01,RACV_INT_MMO_Consideration,4136140,RACV - Business Loans Digital FY17 - NEW - 16.11.16,10647172,Accuen Australia,Business Loans_OMDP_DBM_Custom Affinity_Leaderboard_728x90_Desktop_P25-54_Performance_,RAC18580-Car-Loans-Digital
-banners-728-x-90,12,,,,Car-Insurance-quote-Step-1,,,,,Step-1,,,;src=6017727;type=racv-0;cat=racv_000;u7=;u8=;u9=;u10=;u11=;u12=;u13=Car-Insurance-quote-Step-1;u16=;u17=;u18=Step-1;u20=;u21=;dc_lat=;dc_rdid=;tag_for_child_directed_treatm
ent=;ord=1936578182591.1943;~oref=https://mymembership.racv.com.au/wps/portal/mqte,0.00,1.00,1,0.000000000
2017-09-01,RACV_INT_MMO_Consideration,4136140,RACV - Business Loans Digital FY17 - NEW - 16.11.16,10647172,Accuen Australia,Business Loans_OMDP_DBM_Custom Affinity_Leaderboard_728x90_Desktop_P25-54_Performance_,RAC18580-Car-Loans-Digital
-banners-728-x-90,12,,,,Car-Insurance-quote-Step-1,,,,,Step-1,,,;src=6017727;type=racv-0;cat=racv_000;u7=;u8=;u9=;u10=;u11=;u12=;u13=Car-Insurance-quote-Step-1;u16=;u17=;u18=Step-1;u20=;u21=;dc_lat=;dc_rdid=;tag_for_child_directed_treatm
ent=;ord=5398916632833.284;~oref=https://mymembership.racv.com.au/wps/portal/mqte/!ut/p/b1/hY3JDoJAEES_iHQPmAGOIATDKg4izIXgEgRZggskfL3oHa1bJa9eAYeUSBSpLBMKCfA2H8oif5Zdm9efzmkmIlWZYxFUQrZGTY-IE7AAUSQzkM4ALkTDf3sbeFF3x_nqAPwL_3D5m665QDpj8q
LTkyCCBFcZq5SudwfdnUwprsbxMTHVrkLlNZ2I7zq9Z-DVi9Rke453e13T7oLhmtDwWrkJVvEG7M0tJA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_2069SKG108QSC0ABT1KOSO00M3/act/id=0/p=backJspName=QFBuote_About_You/p=javax.portlet.action=backActionSubmit/365511963564
/-/,0.00,1.00,1,0.000000000

4 个答案:

答案 0 :(得分:0)

由于您还没有向我们展示示例数据和示例预期输出,因此根据您的声明和假设,例如标题位于第一行,而您的页脚位于Input_file的最后一行,可能会对您有所帮助同样的。 让我们说以下是Input_file:

cat file_test
My header
bla bla bla bla data data data
bla bla bla bla

以下是跳过第1行和最后一行(分别为页眉和页脚)的代码。

awk -v lines=$(wc -l < file_test) 'NR>1 && NR<lines' file_test
bla bla bla bla data data data
bla bla bla bla

PS: 如果您还有其他要求,请在帖子中的代码标记中发布带有预期输出的示例Input_file。

答案 1 :(得分:0)

使用awk,您可以从记录print&gt; n 开始NR,但如果要停止{{1},则必须提供记录数量在某些记录之后。另一种方法是将最后的 n 记录保留在缓冲区中(仅在一个记录缓冲区print下面,如之前的)。但首先是一些测试文件:

p

然后脚本及其输出:

$ cat file
head1      # unwanted
head2      # wanted
record1    # wanted
record2    # wanted
footer     # unwanted

修改

使用该特定数据(记录拆分和全部),请使用:

$ awk 'NR>2{print p}{p=$1}' file
head2
record1
record2

答案 2 :(得分:0)

这是使用sed

的另一个想法
sed -n '/Report Fields/,$!b;//d;$!p' file

简要说明,

  • /Report Fields/,$ !b:如果行不在范围内,则跳过脚本 “报告字段”到最后一行
  • //d:删除包含“报告字段”的行
  • $!p:打印除最后一行之外的剩余行

答案 3 :(得分:0)

对于给定的输入,下面的一个应该起作用

awk  'f{sub(/^[ \t]+/,"")}f && !/Grand/; /Report Fields/{f=1}' infile

或者甚至

awk  'f && sub(/^[ \t]+/,""); /Report Fields/{f=1}' infile

<强>解释

  • /Report Fields/{f=1}如果awk找到带有模式的行,请设置变量f=1

  • f{sub(/^[ \t]+/,"")}如果设置了变量f,则在行的开头禁止空格标签字符。

  • f && !/Grand/;如果设置了变量f,并且行不包含Grand,则打印此行。

  • f && sub(/^[ \t]+/,"");如果设置了变量fsub()将返回布尔值true,然后打印此行/记录(以便跳过包含Grand Total:的行,如行与/^[ \t]+/

  • 不匹配