简体   繁体   English

使用awk删除或删除列

[英]Drop or remove column using awk

I wanted to drop first 3 column; 我想放弃前3列;

This is my data; 这是我的数据;

DETAIL 02032017 

Name    Gender  State   School  Class
A          M    Melaka  SS  D
B          M    Johor   BB  E
C          F    Pahang  AA  F
EOF 3               

I want my data like this: 我想要这样的数据:

DETAIL 02032017             
School  Class
SS       D
BB       E
AA       F
EOF 3   

This is my current command that I get mycommandoutput : 这是我当前获得mycommandoutput的命令:

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

Can anyone help me? 谁能帮我?

Thank you :) 谢谢 :)

I want to remove first three column 我想删除前三列

If you just want to remove the first three columns, you can just set them to empty strings, leaving alone those that don't have three columns, something like: 如果你只是想删除前三栏,你可以将其设置为空字符串,独自留下那些不具有三列,是这样的:

awk 'NF>=3 {$1=""; $2=""; $3=""; print; next}{print}'

That has the potentially annoying habit of still having the field separators between those empty fields but, since modifying columns will reformat the line anyway, I assume that's okay: 这有一个令人烦恼的习惯,就是在那些空字段之间仍然有字段分隔符,但是,因为修改列无论如何都会重新格式化,我认为没关系:

DETAIL 02032017
   School Class
   SS D
   BB E
   AA F
EOF 3

If awk is the only tool being used to process them, the spacing won't matter. 如果awk是用于处理它们的唯一工具,则间距无关紧要。 If you do want to preserve formatting (meaning that the columns are at very specific locations on the line), you can just get a substring of the entire line: 如果您确实希望保留格式(意味着列位于行上非常特定的位置),您只需获取整行的子字符串:

awk '{if (NF>=3) {$0 = substr($0,25)}; print}'

Since that doesn't modify individual fields, it won't trigger a recalculation of the line that would change its format: 由于这不会修改单个字段,因此不会触发重新计算将更改其格式的行:

DETAIL 02032017
School Class
SS  D
BB  E
AA  F
EOF 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM