2017-10-06 103 views
-1

我想在awk中得到更好,並使用它的一些CSV格式的文件,它有多個標題行與n行後實際報告開始。該報告還有一個需要丟棄的頁腳。以下是我想結合條件生成一個乾淨的報告awk打印行與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 

與上述我能夠擺脫頁腳,但標題存在。這裏是興田樣本數據

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 
+3

請表明樣品輸入和預期輸出 –

+0

除了Akshay要求發佈樣本數據(您應該將代碼標籤放入您的文章中)之外,我注意到END塊中的條件NR> 2對於所有行都無法正常工作,因爲END一旦完成讀取Input_file的所有行,就會出現awk部分。 – RavinderSingh13

+0

感謝您添加示例輸入,期望的輸出是什麼,請在代碼標記中添加 – RavinderSingh13

回答

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

通常在進行downvoting時發表評論。 (這不是與downvote相關的評論:) –

+1

我認爲有人低估了,因爲你(我們)回答了一個未完成的問題。我也可能與您的輸出相關,而不是反映最後一條記錄未打印。 –

+0

@JamesBrown,同意,謝謝先生。 – RavinderSingh13

0

用awk就可以開始print從記錄NR>ň荷蘭國際集團,但你必須要提供的記錄數,如果你想以後一定的記錄停止print ING。另一種方法是將最後的記錄保存在緩衝區中(如之前的所示,只有一個記錄緩衝區p)。但首先一些測試文件:

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

然後用其輸出的腳本:

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

編輯

與特定數據(與記錄分割和所有),使用此:

$ awk 'NR>=34{print p}{p=$0}' file 
0

這是另一個想法sed

sed -n '/Report Fields/,$!b;//d;$!p' file 

簡要說明,

  • /Report Fields/,$ !b:跳過腳本如果行不 「報告字段」中的最後一行
  • //d範圍:刪除該行包含「Report Fields」
  • $!p:打印除最後一行之外的其餘行
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]+/,"");如果變量f被設置,sub()返回布爾真值,然後打印這樣的行/記錄(因此與Grand Total:該線將被跳過,因爲線不匹配/^[ \t]+/