Steven Lu
Steven Lu

Reputation: 43517

sed path parsing is not working

pwd | sed "s%^\(/[^/]*/\).*?\(/[^/]+\)$%\1...\2%"

I am not sure why this does not work. I have tried both the greedy and non greedy star after the first capture group. I am not even using lookaheads or anything. It works in regex testers. I'm trying to grab the first and last text-part of a path (to squish it while still providing an idea of what dir I'm in).

This is for a tmux prompt line so I'm kind of trying to avoid bringing in something heavyweight like perl to do the job.

Upvotes: 1

Views: 95

Answers (2)

kojiro
kojiro

Reputation: 77157

Why not use parameter expansions? Parameter expansions cost way less processing power than a command, pipe and external command you have there:

start="${PWD#/}"; start="${start%%/*}"
end="${PWD/*\//}"
printf '/%s/…/%s $ ' "$start" "$end"

Upvotes: 1

Steven Lu
Steven Lu

Reputation: 43517

I got it after a few more tries:

pwd | sed "s%^\(/[^/]*/\).*\(/[^/]*\)$%\1...\2%"

Upvotes: 0

Related Questions