简体   繁体   中英

Drop or remove column using awk

I wanted to drop first 3 column;

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 :

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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