简体   繁体   中英

Remove column using awk

I want to remove column,

this is my command:

awk -F"|" -v OFS="|" '{ $1=$2=$3"" ; $0=substr($0,37) } 1' PURCHASE_testing.csv > testing.csv

My input:

Account num|Car|Type|Name|Class|Price
xxxxxxxxxx |A  |Honda| Fara |A|rm20k
xxxxxxxxxx | B | Proton|Afiq |B|rm40k

I managed to remove first 3 column in row 1 but in row 2 and 3 the string not manage to remove.

My output:

       Name      Class      Price              
0101   Fara       A          rm2
1234   Afiq       B          rm4

I want output be:

Name Class Price
Fara   A   rm2
Afiq   B   rm4

To print all but first 3 columns use the following approach:

For the input PURCHASE_testing.csv file contents:

Account num|Car|Type|Name|Class|Price 
0101 |A |Honda| Fara |A|rm20k 
1234 |B | Proton|Afiq |B|rm40k

awk -F"|" '{$1=$2=$3=""; print $0}' PURCHASE_testing.csv > testing.csv

Now, the testing.csv file contents should look like:

Name Class Price 
 Fara  A rm20k 
Afiq  B rm40k

try this -

awk 'BEGIN{FS="|"} {print $4,$5,substr($6,1,4)}' f
Name Class Pric
Fara  A rm20
Afiq  B rm40

Explanation : substr($6,1,4) fetching 1 to 4 character from column 6.

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