Cro2015
Cro2015

Reputation: 303

Awk: splitting a field

new to awk and try to do something that's probably simple but it's taking me a while. To simplify things, I have a text file called 'sample' and it contains the following line:

164516454242451BX%Apt 110 225 1784 Ohio USA

I want to get the following output using awk:

Apt 110 225

Is there a way I can split $1 so that "Apt" is a separate field? The code I'm trying is the below. I recieve no error, but the output is just 2 blank lines.

awk '
BEGIN {
split($1," ","%")
}
print $2,$3,$4
END {
print ""
} ' sample

Upvotes: 5

Views: 9990

Answers (1)

P.P
P.P

Reputation: 121427

You can % as one of the delimiters:

awk -F'[ %]' '{print $2, $3, $4}' file

The same can be done using split as well:

awk '{split($1,a,/%/); print a[2], $2, $3}' file

Upvotes: 13

Related Questions