Linux command

Discussion in 'OT Technology' started by hurleyint1386, Sep 23, 2008.

  1. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Hey, from the command line, how could I search through a file and replace all characters with another? Lets say I want to get rid of colons and replace them with spaces?
     
  2. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    man tr
     
  3. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Thanks
     
  4. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Helping people ever day.
     
  5. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    vi the file and use search and replace.

    :%s:<search phrase>:<replace phrase>:g

    that'll search every line (replace % with <start line #>,<end line #> to limit it to a specific set of lines) and replace every instance of <search phrase> with <replace phrase> (drop the g if you only want to replace the first instance you come to on a line).

    EDIT: just in case you don't know, when you are done type :wq to write the changes and quit. You should definitely learn vi if you are going to be working in any UNIX-like OS.
     
  6. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    FUCK VI
     
  7. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    s/ever/every/
     
  8. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    that's a lot of work when you can just use tr
     
  9. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    i used to use forward slashes until i realized that you can use colons as well. when constantly working on disk devices that have /dev/dsk/ in there, having to escape out all of the /'s is a pain. so, if i have : in my search or replace i use /, otherwise i use :.

    i'm not a fan of tr unless i want to swap multiple spaces with a single character (like a comma to make comma separated text files) or to switch all to upper or lower. either that or swapping a single character with another. if i'm swapping words or multiple character strings in a single command line, i use sed -e "s:ever:every:g".


    oh, and vi is the breast. i even use it for my command line editor. when i use bash, i always start it with bash -o vi. i never did bother with emacs or any other editors though. vim is nice for developing i guess.
     
  10. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    depending on what i'm doing i'm usually a # or ! person myself :o
    s#stuff#stuff#
    s!stuff!stuff!
     
  11. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    lol, really? i used / for years simply because that's what i was taught. later on i found out that you can pretty much use whatever and decided that : made the most sense for me.
     

Share This Page