using Sed on macos to search and replace

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

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    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

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    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.

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

    Joined:
    Feb 26, 2016
    #3
    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 -

    Code:
     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 https://www.gnu.org/software/sed/manual/sed.html
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    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.
     

Share This Page