簡體   English   中英

在awk命令中刪除列

[英]Remove column in awk command

我想刪除列,因為現在我有一個命令,其中包括拆分,標題和尾部命令。 我想在一個命令中進行拆分,標題,尾部並刪除前三列

我的原始輸出是

  Country   Gender    Plate       Name        Account Number        Car Name
    X        F          A          Fara        XXXXXXXXXX            Ixora
    X        F          b          Jiha        XXXXXXXXXX            Saga
    X        M          c          Jiji        XXXXXXXXXX            Proton

我的分割檔案:

第一個檔案

06032017

  Country   Gender    Plate       Name    Account Number     Car Name        
    X        F          A     Fara        XXXXXXXXXX         Ixora

EOF 1

第二檔
06032017

  Country   Gender    Plate       Name        Account Number        Car Name
    X        F          b          Jiha        XXXXXXXXXX            Saga

EOF1

我的願望輸出:

06032017

    Name    Account Number     Car Name        
    Fara        XXXXXXXXXX         Ixora

EOF 1

06032017

  Name        Account Number        Car Name
  Jiha        XXXXXXXXXX            Saga

EOF1

這是我的分割命令:

awk -v date="$(date +"%d%m%Y")" -F\| 'NR==1 {h=$0; next} 
{file="CAR_V1_"$1"_"$2"_"date".csv"; print (a[file]++?"": "DETAIL "date"" ORS h ORS) $0 > file}
END{for(file in a) print "EOF " a[file] > file}' HIRE_PURCHASE_testing.csv

要了解如何跳過某些字段,請執行以下操作:

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{print $0}'
Field1 Field2 Field3 Field4 Field5 Field6  #No skipping here. Printed just for comparison

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{print substr($0, index($0,$4))}'
Field4 Field5 Field6

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{$1=$2=$3="";print}'
   Field4 Field5 Field6
#Mind the gaps in the beginning. You can use this if you need to "delete" particular columns.
#Actually you are not really delete columns but replace their contents with a blank value.

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{gsub($1FS$2FS$3FS$4,$4);print}'
Field4 Field5 Field6   #Columns 1-2-3-4 have been replaced by column4. No gaps here.

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{print $4,$5,$6}' 
Field4 Field5 Field6
# If you have a fixed number of fields (i.e 6 fields) you can just do not print the first three fields and print the last three fields

適應您現有的代碼,該行

print (a[file]++?"": "DETAIL "date"" ORS h ORS) $0 > file

如果更改為

print (a[file]++?"": "DETAIL "date"" ORS h ORS) substr($0, index($0,$4)) > file

或者

print (a[file]++?"": "DETAIL "date"" ORS h ORS) $4,$5,$6 > file #if you have a fixed number of columns in the file)

應該足夠了。

下次您需要幫助時,我建議您簡化問題以更輕松地獲得幫助。 對於其他用戶,很難完全關注您。

也可以在這里查看所有這些功能的詳細信息: https : //www.gnu.org/software/gawk/manual/html_node/String-Functions.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM