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).
We know that the sed on Mac OS is the POSIX sed, which cannot use many options. On the other hand, GNU sed is very convenient. For example, GNU sed interprets escape sequences like \t , \n , \001 , \x01 , \w , and \b . OSX's sed and POSIX sed only interpret \n (but not in the replacement part of s ).
SED is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits, SED works by making only one pass over the input(s), and is consequently more efficient.
In sed, p prints the addressed line(s), while P prints only the first part (up to a newline character \n ) of the addressed line. If you have only one line in the buffer, p and P are the same thing, but logically p should be used.
OSX (BSD) sed doesn't support functions \L
, \E
etc. Install gnu sed
on Mac using this option:
brew install gnu-sed
awk
alternative:
ls | awk '{print $0 tolower(substr($0,length($0)-1,2))}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With