Reputation: 5803
Is there an easy way to split a field, get the first element from the said split field AND also print other fields in same awk?
I want to modify below such that is works for $2 not just when it's MCU:123
but also when it's DISNEY:654
such that $2 returns MCU
or DISNEY
depending on the string. Obviously substr was a bad choice there.
awk 'BEGIN{FS=OFS="\t"}{print $1,substr($2,1,3)}'
Sample of two fields tab seperated
1 mcu:1234
2 disney:234
Expected result tab seperated
1 mcu
2 disney
Upvotes: 0
Views: 718
Reputation: 203189
$ cut -d: -f1 file
1 mcu
2 disney
or if the real file is more convoluted than the example in your question then maybe this will do what you really need:
$ awk -F'[\t:]' -v OFS='\t' '{print $1, $2}' file
1 mcu
2 disney
Upvotes: 2
Reputation: 5803
I was surprisingly closer than I gave my myself credit so throwing this in there as an alternative.
awk 'BEGIN{FS=OFS="\t"}{print $1,substr($2,1,index($2,":")-1)}'
Note that this fails if $2 isn't : delimited. For my use case, $2 would be : delimited by design and even if wasn't, I only need to perform this for values in $2 that do have : in them. To account for that, I could modify this to
awk 'BEGIN{FS=OFS="\t"}{if($2 ~ ":") print $1,substr($2,1,index($2,":")-1)}'
Other solutions are more scalable and possibly more performant.
Upvotes: 1
Reputation: 133428
1st solution: Could you please try following. Written and tested with shown samples.
awk 'BEGIN{FS=OFS="\t"} {sub(/:.*/,"",$2)} 1' Input_file
2nd solution: Using split
function of awk
.
awk 'BEGIN{FS=OFS="\t"} {split($2,arr,":");$2=arr[1]} 1' Input_file
Upvotes: 2