MattJayC

Q: 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

Close

Q: Merge sed coding?

  • All replies
  • Helpful answers

  • by VikingOSX,Solvedanswer

    VikingOSX VikingOSX Oct 8, 2015 2:12 PM in response to MattJayC
    Level 7 (20,819 points)
    Mac OS X
    Oct 8, 2015 2:12 PM in response to MattJayC

    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'

  • by MattJayC,

    MattJayC MattJayC Oct 8, 2015 2:22 PM in response to VikingOSX
    Level 1 (5 points)
    Desktops
    Oct 8, 2015 2:22 PM in response to VikingOSX

    That seams to work for me,

     

    I can't remember but I think it first removes empty lines, then removes tabs/spaces at the start of lines then removes lines that begin with a number.

     

    Thanks again

    Matt

  • by Tony T1,Helpful

    Tony T1 Tony T1 Oct 9, 2015 1:23 AM in response to MattJayC
    Level 6 (9,249 points)
    Mac OS X
    Oct 9, 2015 1:23 AM in response to MattJayC

    To overwrite the original, with no backup, use -i ""

    To append commands, use -e

    Try:

         sed -i "" -e 's/^[ \t]*//' -e '/^\s*$/d' -e '/^[0-9]/ d'

  • by MattJayC,

    MattJayC MattJayC Oct 9, 2015 1:23 AM in response to Tony T1
    Level 1 (5 points)
    Desktops
    Oct 9, 2015 1:23 AM in response to Tony T1

    Many Thanks that helps too!!

     

    Just realised something else, I use this as a list for renaming, sometimes there are some illegal characters, namely there are some "/" can that be replaced with " - " can that be done for all illegal characters?

     

    Many Thanks

    Matt

  • by VikingOSX,

    VikingOSX VikingOSX Oct 9, 2015 4:49 AM in response to MattJayC
    Level 7 (20,819 points)
    Mac OS X
    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.

  • by MattJayC,

    MattJayC MattJayC Oct 9, 2015 6:53 AM in response to VikingOSX
    Level 1 (5 points)
    Desktops
    Oct 9, 2015 6:53 AM in response to VikingOSX

     

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

     

     

    I've opted for this one but in my script it works but duplicates the line.

     

    I'm encapsulating it into applescript so looks like this

     

    set thescript to "sed -i \"\" -e 's/^[ ]*//' -e '/^\\s*$/d' -e '/^[0-9]/ d' -e  '/Introduction/d' -e  '/Conclusion/d' -e s'/[\\/\\*]/-/gp' "

  • by VikingOSX,

    VikingOSX VikingOSX Oct 9, 2015 6:56 AM in response to MattJayC
    Level 7 (20,819 points)
    Mac OS X
    Oct 9, 2015 6:56 AM in response to MattJayC

    Drop the trailing p then. Don't you just love those AppleScript escapes to distract readability.

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert Oct 9, 2015 7:02 AM in response to VikingOSX
    Level 5 (4,649 points)
    Oct 9, 2015 7:02 AM in response to VikingOSX

    Hi VikingOSX,

     

    Characters within a bracket expression do not need to be escaped but you have to be careful in which order you place special characters within the expression. Overall it is much safer to use POSIX character classes, ie

     

    sed 's/[[:punct:]]/-/g'

  • by MattJayC,

    MattJayC MattJayC Oct 9, 2015 7:05 AM in response to VikingOSX
    Level 1 (5 points)
    Desktops
    Oct 9, 2015 7:05 AM in response to VikingOSX

    I'm just glad thats the one little thing I have learnt took me ages to figure out how to make it work!

  • by VikingOSX,

    VikingOSX VikingOSX Oct 9, 2015 10:08 AM in response to Mark Jalbert
    Level 7 (20,819 points)
    Mac OS X
    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.