sed and Mac OS X differences with to upper, to lower and whole capture control sequences

旧街凉风 提交于 2020-02-13 04:51:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!