using Sed on macos to search and replace

Discussion in 'iOS Programming' started by theprizerevealed, Oct 19, 2018.

  1. theprizerevealed macrumors member

    Feb 26, 2016
    I need to add apostrophes to a bunch of filenames. I think that maybe Sed is the best way to do this but it's years since I used it and I forgot almost everything.

    So these file names vary in length and other things, though they are separated by newlines in a text file such as:

    my cat is calico
    my cat is fat and happy
    there are 3 cats in my house
    their names are - Pantalaimon Serenity

    What are the symbology that I need to capture the lowercase, uppercase, numbers, and other symbology?

    I know it's generally something such as this:

    sed -i 's/my cat is calico/"my cat is calico"/g' filename.txt

    but obviously I need to generalize it to capture all filenames in the file
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    1. get rid of the white space at the end of the Serenity line.
    2. Wrap quots around the text of each line.
    3. Direct the altered content to a new file.

    sed -e 's/[[:space:]]*$//' -e 's/^.*$/\"&\"/' < filename.txt > filename2.txt
  3. theprizerevealed, Oct 19, 2018
    Last edited: Oct 19, 2018

    theprizerevealed thread starter macrumors member

    Feb 26, 2016
    thanks! Could you elaborate on the -e option flag? I can't find any information about it. Actually, if it isn't too much trouble, help me to understand each character? :p The *$ does that represent the entire filename? and what about ^.$ and &\ ?

    I'm trying to find a good online resource to remind me of all this stuff but I guess I need to buy a book...

    Does ^ represent each line?
    --- Post Merged, Oct 19, 2018 ---
    I was able to make this part work -

     sed 's/^.*$/\"&\"/'  filename.txt 
    But the output went to the terminal screen instead of overwriting the original file...
    --- Post Merged, Oct 19, 2018 ---
    For any future readers of this thread - I found this to help
  4. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    The -e means the following is one of many sed commands to execute.

    The stuff in the first half of each command is a regular expression.

    The asterisk indicates to match zero or more of the preceding character.

    The [[:space:]] refers to white space. If I recall right, that includes the space character of course, but also includes things like tabs and line feeds.

    The $ sign indicates the end of the line being tested. So in the first command, I’m looking at replacing one or more spaces that occur at the end of the line. Your sample test has a line like that.

    The period means to match any character, so combined with he asterisk, we are matching zero or more of all characters.

    The caret ^ means to test from the beginning of the line. So in the second command, I’m capturing all text from the beginning (^) of the line to the end ($). The and (&) symbol in the second part of that statement is using the captured test from the first part of that command. (I had to look that up) I’m then wrapping it with quote marks.

    The less than sign and greater than signs are for input and output. So I’m outputting to a new file. Do NOT output to the same name as the input file.

    Open terminal and type ‘man sed’ For more info.

    I believe the awk command can do all of the above against a single input file. I haven’t played with that though.
  5. theprizerevealed, Nov 14, 2018
    Last edited: Nov 14, 2018

    theprizerevealed thread starter macrumors member

    Feb 26, 2016
    Okay, I'm experiencing trouble using Sed yet. I have another list that I need to arrange differently so I can see it more clearly. Basically there's a dictionary I'm trying to use which has duplicate keys.

    The list is very long and wraps around to the next line. So I'm trying to used sed to separate it by new line so I can find the duplicate more easily.

    Just as an example I have:

    "test": ["everything"], "Next": ["anything2"], "last1": ["4all"],

    and I want:

    "test": ["everything"],
    "Next": ["anything2"],
    "last1": ["4all"],

    so the command I'm trying to use is:

    sed 's/\[.*\]\: \[.*\]\,/\[.*\]\: \[.*\],\n/'

    or maybe? 's/.*\: .*,/.*\: .*,\n/'
  6. xStep, Nov 16, 2018
    Last edited: Nov 16, 2018

    xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Think it through. What is the minimum for the delimiter between the items of separation. If any of your quoted strings can have commas, then the delimiter is more than just the comma. Focus on the delimiter.
  7. theprizerevealed thread starter macrumors member

    Feb 26, 2016
    After much MUCH searching and a great deal of frustration I have found the solution here:

    and also here:

    For the sake of helping others I decided to post my solution here.

    this was the command I used:

       sed -e 's/, /\'$'\n/g' < duplicates.txt > outfile.txt  
    --- Post Merged, Jan 3, 2019 ---
    I have read several accounts that Sed has become outdated severely. Is there a more recent up to date unix tool with similar capabilities?
  8. theprizerevealed thread starter macrumors member

    Feb 26, 2016
    I should note that my solution was for a dictionary with keys with single values, not arrays of values. Incidentally, where could I write to the Apple MacOS developers to ask them to update the Sed tool to the latest version which is apparently from 2009?
  9. PhoneyDeveloper macrumors 68040


    Sep 2, 2008

Share This Page