Results 1 to 8 of 8

Thread: sed to delete the 2nd throgh 14th of characters in a line

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    801
    Distro
    Ubuntu 14.04 Trusty Tahr

    sed to delete the 2nd throgh 14th of characters in a line

    Hi Gurus,

    I was wondering how I could use sed. or another command if it is better suited for the task, to delete a range of characters from each line in a file.

    Specifically I am using todo.txt to keep a list of todo items that I display in conky.
    I have a script that will take an item from my todo.txt file and copy it in remind format into my reminders file, so I get a popup reminding me to do the task.

    Right now the todo2remind script is grabbing the entire todo item.

    Example:
    Code:
    1 2014-06-01* (E) +conputer Fix Mutt's spell check
    What I would like is to have only the actual message grabbed. Since I need to know the line number, in this case 1, I'd like to remove the 2014-06-01* (E) (this is different in every line, but the position is the same)
    which would leave:
    Code:
    +conputer Fix Mutt's spell check
    Even better would be to get rid of the plus sign and the word that comes directly after it as well (+computer, in this case).
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,493
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    Code:
    $ echo "1 2014-06-01* (E) +conputer Fix Mutt's spell check" | sed 's/[0-9*-]*\s*([A-E])\s*+\w*\s*//'
    1 Fix Mutt's spell check
    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

  3. #3
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    801
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    Thanks man!
    How can I save the output as a variable to use elsewhere?
    I thought I knew, but keep getting errors.
    I mean the:
    Code:
    Fix Mutt's spell check

    After looking at what I am trying to do, I realized that I don't need to keep the line number after all.


    Last edited by GrouchyGaijin; May 1st, 2014 at 05:14 PM.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,493
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    capture the output with $()

    Code:
    stuff=$( sed 's/.*([A-E])\s*+\w*\s*//' file )
    but do you really need to store it? what's it for exactly?
    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
    Oct 2010
    Location
    Sweden
    Beans
    801
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    Hmmm I must be making this more difficult than it needs to be.
    The basic idea is that I can take a todo item from my todo.txt file and have it added to my reminders file for use with remind which generates a popup notification.

    Here is the original script:

    Code:
    #!/bin/bash
    cat -n ~/Dropbox/todo/todo.txt
    read -p "Enter the line number of the task to add to remind " task
    sed -n "$task"p ~/Dropbox/todo/todo.txt
    message=$(sed -n "$task"p ~/Dropbox/todo/todo.txt)
    read -p "What is the trigger date of the reminder?:" triggerdate
    read -p "H0w many days in advance should this appear?" daysadvance
    read -p "What is the trigger time of the reminder?:" triggertime
    echo "REM $triggerdate +$daysadvance AT $triggertime MSG $message %b (in [_countdown(trigdatetime()-current())])" >> ~/Reminder-files/reminders-general
    This works but right now I am getting:
    Code:
    1 2014-06-01* (E) +conputer Fix Mutt's spell check
    in my reminder file when I only really want
    Code:
    Fix Mutt's spell check
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,493
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    Code:
    #!/bin/bash
    
    todo=~/Dropbox/todo/todo.txt
    reminders=~/Reminder-files/reminders-general
    
    cat -n "$todo"
    read -p "Enter the line number of the task to add to remind: " task
    message=$( sed -n "$task s/.*([A-E])\s*+\w*\s*//p" "$todo" )
    echo "$message"
    read -p "What is the trigger date of the reminder?: " triggerdate
    read -p "How many days in advance should this appear?: " daysadvance
    read -p "What is the trigger time of the reminder?: " triggertime
    echo "REM $triggerdate +$daysadvance AT $triggertime MSG $message %b (in [_countdown(trigdatetime()-current())])" >> "$reminders"
    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

  7. #7
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    801
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    Thanks again Vaphell!

    It is this part:
    Code:
    $( sed -n "$task s/.*([A-E])\s*+\w*\s*//p" "$todo" )
    that I don't fully understand.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,493
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sed to delete the 2nd throgh 14th of characters in a line

    you can apply line numbers or regexes to s/// just like you can apply them to p or d.
    1p - print 1st line
    1d - delete first line
    1s/// - modify first line

    /regex/ p - print matching line(s)
    /regex/ d - delete matching line(s)
    /regex/ s/// - modify matching line(s)

    If you want to narrow down to a single line, just put the number in front of s/// and you are golden.
    p at the end is to print the result of s/// to offset that -n option.
    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

Tags for this Thread

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
  •