I would like to precede each line with a number saying how many slashes the line has got.
awk '{ l=$0; gsub("[^/]","",l); print length(l),l }'
This doesn't work bacause l=$0 seems to assign by reference.
How do I dup the string?
Is there a better way to do this with standard UNIX tools? I essentially want to sort a list of filepaths by depth (slash count).
git ls-files, but I'm solving my problem differently in the end. At least I learned something aboutawk.l, then printingl. Why would you expect it to be unchanged? Presumably, you meant to doawk '{l=$0; gsub("[^/]","",l); print length(l),$0}which does what you want.awk '{l=$0; gsub("[^/]","",l); print length(l),$0}but wroteawk '{l=$0; gsub("[^/]","",l); print length(l),l}. I hate these typos.