Results 1 to 7 of 7

Thread: Getting creative with SED - removing string and x digits after

  1. #1
    Join Date
    Dec 2012
    Beans
    71

    Getting creative with SED - removing string and x digits after

    Hi, HELP!

    i have a file i'd like to shrink in size a little, and as the date appears twice, i've decided thats a good start.

    the file appears as:

    < Jun 17 00:00:00 - some data

    I'm using sed -e 's/.*$mmm $dd //' myfile

    This removes everything up to ' 00:00:00 - '

    how can i get rid of the time too, is there any way i can amend the above statement to either remove the next x digits (9 in this case) or make 00:00:00 some form of formatted wildcard?

    Thanks in advance

    Chris

  2. #2
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Getting creative with SED - removing string and x digits after

    Thread moved to "Programming Talk" sub-forum.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  3. #3
    Join Date
    Mar 2010
    Location
    India
    Beans
    7,305
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting creative with SED - removing string and x digits after

    Quote Originally Posted by cbillson View Post
    the file appears as:

    < Jun 17 00:00:00 - some data

    I'm using sed -e 's/.*$mmm $dd //' myfile

    This removes everything up to ' 00:00:00 - '

    how can i get rid of the time too, is there any way i can amend the above statement to either remove the next x digits (9 in this case) or make 00:00:00 some form of formatted wildcard?
    I'm not a programming guy and can't understand how the $mmm $dd is working in your example above (is it a predefined variable in your file?). But here's a variation of the sed command that works for the above example (can't say if it has a caveat) -
    Code:
    sed 's/.* \([0-9]*:.*\)/\1/'
    (you don't really need the "-e" option unless the command/script is not the first argument after sed)

    I tested it on -
    Code:
    echo "Jun 17 00:00:00 - some data" | sed 's/.* \([0-9]*:.*\)/\1/'
    Returns -
    00:00:00 - some data
    EDIT:
    Just to be clear, my approach above is to 'keep' a desired part (everything between the "\(..and..\)) and drop the rest.
    You can divide a string/line in more than one such groups and keep them by referring as \1, \2, \3 etc.
    Last edited by varunendra; June 17th, 2013 at 12:23 PM.
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,243
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting creative with SED - removing string and x digits after

    read about regexes, if you need to manipulate text from time to time, few hours you put into reading is about the best long term investment you can possibly make.

    you can match digit with [0-9] (range of chars 0-9), capital letters [A-Z], all letters [A-Za-z] etc

    if you want to simply remove a part of string
    Code:
    $ data="Jun 17 00:00:00 - some data"
    $ echo "$data" | sed -r 's/[A-Z][a-z]{2} [0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} - //'
    some data
    i use sed with -r option that makes regexes more readable (fewer \ to achieve cool stuff)

    as poster above mentioned you can also capture parts of the text and reuse them in a format of your choice, eg
    Code:
    $ echo "$data" | sed -r 's/.*([A-Z][a-z]{2} [0-9]{1,2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) - (.*)/**\1**\3**\2/'
    **Jun 17**some data**00:00:00
    Last edited by Vaphell; June 17th, 2013 at 03:02 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Dec 2012
    Beans
    71

    Re: Getting creative with SED - removing string and x digits after

    Thanks for the info, makes total sense, and could prove very useful in some other things i'm trying to do - cheers

  6. #6
    Join Date
    Mar 2010
    Location
    India
    Beans
    7,305
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting creative with SED - removing string and x digits after

    Quote Originally Posted by cbillson View Post
    Thanks for the info, makes total sense, and could prove very useful in some other things i'm trying to do - cheers
    Try and have fun with it. And pleas consider marking the thread as [Solved] unless you have any more questions on the topic. If you have questions on a different topic, start a new thread.

    Thanks !
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags

  7. #7
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: Getting creative with SED - removing string and x digits after

    This would remove everything up to and including the first ' - '.
    Code:
    sed 's/.* - //'
    Cheesemill

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
  •