Results 1 to 7 of 7

Thread: Grep / awk

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    617
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Grep / awk

    Hi Guys,

    I have a decent command line script that will let me add, edit and view my Google tasks.
    I've fed the output into Conky using kaivalagi's conkyText script.


    This gives me:

    Code:
    GrouchyGaijin's list
       0 [ ] Test Task 
         Due Date: Fri, May 03, 2013
       1 [ ] Another Test Task
         Due Date: Fri, May 03, 2013
    Is there a way I can automatically change the output of the script to go from the above example to
    Code:
    0; Test Task; Due Date: Fri, May 03, 2013
    1; Another Test Task; Due Date: Fri, May 03, 2013
    Or does that fact that the length and content of the output will change depending on the tasks make it impossible?
    Last edited by GrouchyGaijin; May 4th, 2013 at 04:08 PM. Reason: solved
    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
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,325
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help from the GREP Gods - extracting text

    Hi GrouchyGaijin.

    I think 'awk' would be more suitable for that job.

    This works (except some trailing spaces):
    Code:
    $ awk '/\[ \]/ {n=$1; $1=$2=$3=""; printf("%s;%s; ",n,$0); getline; print $0}'  file.txt
    
    0;   Test Task;      Due Date: Fri, May 03, 2013
    1;   Another Test Task;      Due Date: Fri, May 03, 2013
    Regards.

  3. #3
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    617
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help from the GREP Gods - extracting text

    Thank you!
    I truly do appreciate the help.

    After I run my script I now get:

    Code:
    0;   Example Task;      Due Date: Fri, May 03, 2013
    1;   Another example task;      Due Date: Fri, May 03, 2013
    2;   Task3;      Due Date: Fri, May 03, 2013
    Which is exactly what I asked for. You truly do rock.
    I was wondering if you could tell me how to remove the 0 1 and 2 at the beginning but keep the semi-colon?
    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
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,325
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help from the GREP Gods - extracting text

    I highlighted the parts that take care of the number:
    Code:
    awk '/\[ \]/ {n=$1; $1=$2=$3=""; printf("%s;%s; ",n,$0); getline; print $0}'  file.txt
    So if you get rid of that:
    Code:
    $ awk '/\[ \]/ {$1=$2=$3=""; printf(";%s; ",$0); getline; print $0}'  file.txt
    
    ;   Test Task;      Due Date: Fri, May 03, 2013
    ;   Another Test Task;      Due Date: Fri, May 03, 2013
    Alternatively, you can also use this other version:
    Code:
    awk '/\[ \]/,gsub("^.*]","") {printf(";%s; ",$0); getline; print $0}'  file.txt
    Let us know if you need more details to understand the code.
    Regards.

  5. #5
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    617
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help from the GREP Gods - extracting text

    Thank you.
    Actually I have no understanding of what you did, but would really like to be able to do this without bothering people for help.
    I'll check the awk man page, but often I simply don't understand the manual.
    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
    Feb 2013
    Beans
    Hidden!

    Re: Grep / awk

    How about
    Code:
    awk -F'\n[ \t]*' -vRS='[0-9]+ *[[] []] *' -vOFS='; ' 'NR>1{print "",$1,$2}' file.txt
    or even
    Code:
    awk -F\\n -vRS='[0-9]+ *[[] []]' 'NR>1,$0=";"$1";"$2' file.txt

  7. #7
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    617
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Grep / awk

    @Schragge Thanks man, once again you come to my coding aid.
    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

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
  •