Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Merge sed coding?

I've been fiddling with some sed, most found through searching around the web, the 3 lines below work for me in that order one after another. I don't need the 3 backup files, if it can overwrite the original that could be great to.

Hope someone can help

Thanks

Matt


sed -i.bkp 's/^[ \t]*//'

sed -i.bkk '/^\s*$/d'

sed -i.bbb '/^[0-9]/ d'

Posted on Oct 8, 2015 2:07 PM

Reply
Question marked as Best reply

Posted on Oct 8, 2015 2:12 PM

What does this do for you? You may need to insert an 's' character after each semi-colon.

sed -i.bkp 's/^[ \t]*//;/^\s*$/d;/^[0-9]/d'

10 replies

Oct 9, 2015 4:49 AM in response to MattJayC

Although the semi-colon can be used to string multiple sed commands together, it reduces readability, and the use of the -e gives your code breathing space, and added comprehension. You should use -e.


There are two ways to approach changing the '/' character to '-' using sed. You can escape the '/', or you can use different separators:

  • 's/\//-/'
  • 's|/|-|'

Using -e, you would simply append either syntax to your existing sed train.


The above is good if you know that you have just one character that needs transformed. Sed also supports changing a collection of characters to a single character, so by example the following will look for '/' and '*' (Sed reserved, must be escaped) and transform their occurrences to '-' for every processed line.

-e s'/[\/\*]/-/gp'


But as far as I know, Sed will not perform multiple replacements without multiple sequences of s/find/replace/g. The tr(1) utility on the otherhand can do many:many character mappings. Send the output of your existing Sed command sequence (train) into tr to perform multiple replacements:

sed train | tr '/*' '-x'

which would replace every occurrence of '/' with '-', and every occurrence of '*' with 'x' on each processed line. You are not limited to two characters.

Oct 9, 2015 10:08 AM in response to Mark Jalbert

Mark,


I am fully aware of the POSIX class syntax, and should have used it, were it not for old habits that continue to haunt my fingers. For some reason, that I now cannot duplicate, sed blew up on me when I used unescaped characters in that bracket previously. That is why they were posted escaped. Had to have been some self-imposed typo.

Merge sed coding?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.