find / replace line breaks in textedit

Discussion in 'Mac Programming' started by nameshock, Mar 14, 2011.

  1. nameshock macrumors newbie

    Joined:
    Mar 14, 2011
    #1
    Is there a way to search for line endings, ¶, in ANY mac text editor so I can say, for instance append a www. or @ symbol infront of 200 lines of domain names?

    ex:

    john.com
    mary.com
    todd.com

    find / replace .com<line-ending-symbol>www.

    >

    www.john.com
    www.mary.com
    www.todd.com

    I can do it on a windows machine with text edit lite, but was wondering if there was a way to do it on a mac?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You want to end up with one huge line? Or you simply want to pre-pend www. to every line. Because if you replace the line end character then there won't be any line breaks afterwards. Anyways you can do it using sed or awk on the command line.

    Edit: assuming your file is called test.txt and you want to output to test1.txt then this will pre-pend www. to very line without wiping out the new lines which is what your example asks for (not replacing as your text says). Note you will have to cd to the directory containing test.txt or use a relative or absolute path.

    Code:
    sed 's/^/www./' <test.txt >test1.txt
    
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    Assuming this is ascii text, you can do it in the terminal like this (although independent of line breaks in this case).

    Code:
    while read line; do echo "www."$line ; done < domain_names.txt > www_added.txt
    
     
  4. jahala macrumors regular

    Joined:
    Feb 7, 2008
    #4
    Pick a more powerful text editor

    Macs ship with emacs and vim accessible via the terminal. You can also download versions that act more like a Mac application.

    MacVim from http://code.google.com/p/macvim/
    emacs choices are listed at http://www.emacswiki.org/emacs/EmacsForMacOS

    Other options can be seen at http://mac.appstorm.net/roundups/office-roundups/top-10-mac-text-editors/

    In vim or emacs you can use the regular expression given in a previous post. I use vim, so I would type in the following in normal mode:
    :%s/^/www./

    I think your best bet would be start using a text editor with a few more features than TextEdit.
     
  5. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #5
    I'm sure the above methods will work, but he asked how to do it in TextEdit. You can paste a line ending into the Find: box, then paste it in the Replace With: box too, followed by your www.

    To capture a line ending, put the cursor at the end of a line, press shift-right arrow to select it, then copy.
     
  6. nameshock thread starter macrumors newbie

    Joined:
    Mar 14, 2011
    #6
    Thanks, yeah I am actually using TextMate and since it was so expensive I figured it could do this.

    I tried shift-right arrow to select, but for some reason (not sure if it is the encoding) it doesn't work. It just thinks that it is some long tab or something.

    And, yes, I am just trying to pre-pend www. to every line
     
  7. subsonix, Mar 14, 2011
    Last edited: Mar 14, 2011

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #7
    It is possible with sed in TextMate: choose Text > Filter Through Command

    and insert: sed 's/^/www./'

    in the text box.

    Hit enter.
     
  8. nameshock thread starter macrumors newbie

    Joined:
    Mar 14, 2011

Share This Page