Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

macos

sed

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).

like image 952
James Robinson Avatar asked Aug 18 '13 10:08

James Robinson


People also ask

Does macOS have sed?

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 ).

What is sed command in Mac?

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.

What is sed P?

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.


2 Answers

OSX (BSD) sed doesn't support functions \L, \E etc. Install gnu sed on Mac using this option:

brew install gnu-sed
like image 96
anubhava Avatar answered Oct 21 '22 02:10

anubhava


awk alternative:

ls | awk '{print $0 tolower(substr($0,length($0)-1,2))}'
like image 27
mschilli Avatar answered Oct 21 '22 01:10

mschilli