问题
I am trying to take the last two letters out of a filename which are uppercase and append them to the filename in lowercase. I expeceted the command:
ls | sed -e "s/.*\([A-Z][A-Z]\)$/\0\/\L\1\E/"
to achieve this and on my Ubuntu box it worked fine but on my Mac it simply prints out a 0/LXXE/
where XX
are the correct letters from the capture.
What are the Mac sed equivalents of \0
, \L
and \E
?
I've had a look around the web and several people have noticed that Mac OS X sed is different from Ubuntu sed but most threads talk about the -i
requirement for a file extension or empty string (which has previously tripped me up).
回答1:
OSX (BSD) sed doesn't support functions \L
, \E
etc. Install gnu sed
on Mac using this option:
brew install gnu-sed
回答2:
awk
alternative:
ls | awk '{print $0 tolower(substr($0,length($0)-1,2))}'
来源:https://stackoverflow.com/questions/18298356/sed-and-mac-os-x-differences-with-to-upper-to-lower-and-whole-capture-control-s