How to extract filename from path using sed or awk
awk, linux, sed, unix
Solution
the input/output in your question is not well formatted. do you need this?
awk '{gsub(/\/.*\//,"",$1); print}' file
test
kent$ echo "/common/common/img/pictos/klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404"|awk '{gsub(/\/.*\//,"",$1); print}'
output:
klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404
Problem
I am trying to parse a filename from a modified apache web access log entry that is tab delimited: ``` /common/common/img/pictos/klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404 ``` I would like it to come out like this: ``` klArrowRight.gif /common/common/img/pictos/klArrowRight.gif 03/Dec/2012:00:00:00 127.0.0.1 03/Dec/2012:00:00:00 us 404 ``` I have tried something like this in sed: 's:.*/::' However, it is too greedy, and it eats the rest of my line. I have been looking through posts, but so far no luck. Any hints?