Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Find and replace in file

  1. #1

    Default Find and replace in file

    Hi,
    I have some large csv files, some above 700 megabytes and i need to find and replace a part of a string. I just read about 'sed' and it's simple command but how do I type the command if I want to replace "/Category" with "old/Cat egory/" without quotes. The / needs to be escaped somehow so that 'sed' doesn't take it as a parameter delimiter right?
    If you guys have some other suggestions on how to edit these large csv files please post them here.
    Libre office works for files under 300 megabytes, but above that it becomes unresponsive.
    Thanks!
    OpenSUSE Leap 42.2 x64 KDE

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,864
    Blog Entries
    15

    Default Re: Find and replace in file

    On Mon 16 Sep 2013 11:16:02 PM CDT, robertot5 wrote:


    Hi,
    I have some large csv files, some above 700 megabytes and i need to find
    and replace a part of a string. I just read about 'sed' and it's simple
    command but how do I type the command if I want to replace "/Category"
    with "old/Cat egory/" without quotes. The / needs to be escaped somehow
    so that 'sed' doesn't take it as a parameter delimiter right?
    If you guys have some other suggestions on how to edit these large csv
    files please post them here.
    Libre office works for files under 300 megabytes, but above that it
    becomes unresponsive.
    Thanks!


    Hi
    Here you go;
    Code:
    find ./ -type f -print -exec sed -i 's/\/Category/old\/Cat egory\//g' {} \;
    --
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    openSUSE 12.3 (x86_64) GNOME 3.8.4 Kernel 3.7.10-1.16-desktop
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!
    Last edited by malcolmlewis; 16-Sep-2013 at 16:41.

  3. #3
    Join Date
    Sep 2012
    Posts
    5,141

    Default Re: Find and replace in file

    Quote Originally Posted by robertot5 View Post
    I want to replace "/Category" with "old/Cat egory/" without quotes. The / needs to be escaped somehow
    You can use any character as delimiter, so
    Code:
    sed -e 's@/Category@old/Cat egory/@'
    will work fine.

  4. #4

    Default Re: Find and replace in file

    Sorry, I typed it wrong,

    I need to replace instances of "Category\" with "Old\Category\" so it is a backwards slash "\".

    I'm getting
    Code:
    sed: -e expression #1, char 40: unterminated `s' command
    for
    Code:
    sed -e 's@Category\@Old\Category\@'
    OpenSUSE Leap 42.2 x64 KDE

  5. #5

    Default Re: Find and replace in file

    Quote Originally Posted by robertot5 View Post
    Sorry, I typed it wrong,

    I need to replace instances of "Category\" with "Old\Category\" so it is a backwards slash "\".

    I'm getting
    Code:
    sed: -e expression #1, char 40: unterminated `s' command
    for
    Code:
    sed -e 's@Category\@Old\Category\@'
    In that case you can just use '/' as separator, but that doesn't matter.

    But you'll have to escape '\' itself (because it's the escape character) by using '\\'.

    So this should work:
    Code:
    sed -e 's/Category\\/Old\\Category\\/'
    or
    Code:
    sed -e 's@Category\\@Old\\Category\\@'

  6. #6

    Default Re: Find and replace in file

    Quote Originally Posted by wolfi323 View Post
    In that case you can just use '/' as separator, but that doesn't matter.

    But you'll have to escape '\' itself (because it's the escape character) by using '\\'.

    So this should work:
    Code:
    sed -e 's/Category\\/Old\\Category\\/'
    or
    Code:
    sed -e 's@Category\\@Old\\Category\\@'
    Bingo, that was it !

    I reached this command

    Code:
    sed -ie 's@Category\\@Old\\Category\\@g' bigfile.csv
    And it does the work in just a few seconds, absolutely amazing, it took me hours to find and replace with other text editors.

    LINUX ROCKS !

    Thank You very much.

    L.E: I understand the "-i" parameter means "in-place" - That means the modifications are made in the original file correct ?
    The "-e" parameter I do not fully understand. Can you please explain it to me in a few words ? I noticed a .csve file was created in the same folder as my big.csv file. Is that the result of the "-e" parameter
    OpenSUSE Leap 42.2 x64 KDE

  7. #7

    Default Re: Find and replace in file

    Oops,
    I reached a csv in wich I need to replace instances of "CITROËN" with "Old\CITROEN" but the character "Ë" give me headaches. It seems to ignore it and nothing is replaced.

    I tried adding the character from character selector tool but I does not work. How can I make SED understand that " Ë " ?
    OpenSUSE Leap 42.2 x64 KDE

  8. #8

    Default Re: Find and replace in file

    Quote Originally Posted by robertot5 View Post
    Oops,
    I reached a csv in wich I need to replace instances of "CITROËN" with "Old\CITROEN" but the character "Ë" give me headaches. It seems to ignore it and nothing is replaced.

    I tried adding the character from character selector tool but I does not work. How can I make SED understand that " Ë " ?
    Well, in this case it seems to be a charset issue.

    I would just try a '.' instead of 'Ë' in the search query, that matches any character. I guess there won't be too many other "CITRO.N" (where '.' != 'Ë') strings in there...

  9. #9
    Join Date
    Sep 2012
    Posts
    5,141

    Default Re: Find and replace in file

    Quote Originally Posted by wolfi323 View Post
    I would just try a '.' instead of 'Ë' in the search query, that matches any character.
    Only if document character set is the compatible with current locale.

  10. #10
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Find and replace in file

    On 2013-09-20 14:06, arvidjaar wrote:
    >
    > wolfi323;2586356 Wrote:
    >> I would just try a '.' instead of 'Ë' in the search query, that matches
    >> any character.

    >
    > Only if document character set is the compatible with current locale.


    Is the source file coded in UTF-8?

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 12.3 x86_64 "Dartmouth" at Telcontar)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •