Kai
Kai

Reputation: 165

Trim a string with AWK

Original format of the file (space separated):

a b http://c.com/?longlongname e f g

I want to trim the 3rd field (http://c.com/?longlongname) into c.com, and keep the rest fields as the same. I would like to use awk to do this task. Could anyone give me any hint?

Upvotes: 0

Views: 281

Answers (1)

Zombo
Zombo

Reputation: 1

awk '{split($3, z, "/"); $3=z[3]} 1'
  • take third field
  • split on /
  • change value of third field to third result of split
  • print line

Result

a b c.com e f g

Upvotes: 3

Related Questions