Results 1 to 6 of 6

Thread: Help with a sed command

  1. #1
    Join Date
    Feb 2007
    Beans
    15

    Help with a sed command

    Hello, I have a text file that is tab delimited with 6 fields, what I need to do is replace the final field. Here's a sample of the text file:

    Code:
    NAVI	Navisite	-0.08	-0.08	-0.09	11-Mar
    NEN	New England Realty	null	null	null	11-Mar AMC
    NPSP	NPS Pharmaceuticals	-0.13	-0.14	-0.18	11-Mar 4:30 PM
    I need to replace the date field with a variable, but lets just use 2010-03-11 for now. The beginning of the field will begin with either 1 or 2 numbers followed by a dash etc... e.g. 9-Mar, or 11-Mar AMC

    Here's what I tried, though it didn't work.
    Code:
    sed 's/[0-9]+(?=-.)/2010-03-11/g'
    Last edited by jamefarm; March 12th, 2010 at 06:33 PM.

  2. #2
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with a sed command

    This seems to work:-
    Code:
    sed 's/[0-9]*-[A-Z][a-z][a-z]/11-03-2010/'
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  3. #3
    Join Date
    Feb 2007
    Beans
    15

    Re: Help with a sed command

    Just about, but I need it to remove anything after that as well, such as the AMC or the time so that the last field is only '2010-03-11'

  4. #4
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with a sed command

    Then this may do the trick:-
    Code:
    sed 's/[0-9][0-9]*-[A-Z a-z 0-9 :]*/11-03-2010/'
    Edit:- Whoops, didn't see that the time was also there at times.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  5. #5
    Join Date
    Feb 2007
    Beans
    15

    Re: Help with a sed command

    Works like a charm! Thanks a bunch PmDematagoda!

  6. #6
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Help with a sed command

    another couple of variations:
    Code:
    ~/tmp$ sed 's/[^\t]*$/11-03-2010/' testfile
    NAVI    Navisite        -0.08   -0.08   -0.09   11-03-2010
    NEN     New England Realty      null    null    null    11-03-2010
    NPSP    NPS Pharmaceuticals     -0.13   -0.14   -0.18   11-03-2010
    ~/tmp$ cut -f1-5 testfile | sed 's/$/\t11-03-010/'
    NAVI    Navisite        -0.08   -0.08   -0.09   11-03-010
    NEN     New England Realty      null    null    null    11-03-010
    NPSP    NPS Pharmaceuticals     -0.13   -0.14   -0.18   11-03-010

Bookmarks

Posting Permissions

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