繁体   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