Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Unanswered: how to delete characters in a file except pattern

    hi all,

    anyone of you help me what command to be used in unix to delete all characters before pattern starts in every line of a text file

    for example, the pattern is "user".

    the file has line like,

    1234|needhelp234|user234

    from the above line, onle user234 to be retained remaining characters to be removed.

    thanks
    balaji

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    sed 's/.*\(user*\)/\1/'

  3. #3
    Join Date
    Apr 2009
    Posts
    2

    Talking

    thanks..it is done...

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ..and the solution you found was?

    ..so that others in your position can find out what to do...
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Quote Originally Posted by healdem
    ..and the solution you found was?
    I think pdreyer had the right solution mark though he may of missed a dot before the * ie
    Code:
    sed 's/.*\(user.*\)/\1/'
    Those brackets remember everything from user onwards and put it into a "variable" called \1 then the whole string is replaced by this variable.

  6. #6
    Join Date
    Jun 2009
    Location
    South Africa
    Posts
    33
    There are many ways to skin a cat..

    As an alternative, you can also use the "cut" command for the same result :

    echo "1234|needhelp234|user234" | cut -f 3 -d "|"
    or
    cat /my/file | cut -f 3 -d "|"

    The cut -f 3 -d "|" says to retain only field number 3 (-f3) of the input, and assume that all fields are delimited with the "|" character (-d"|").

Posting Permissions

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