Results 1 to 10 of 10

Thread: bash grep csv dilemea

  1. #1
    Join Date
    Aug 2013
    Beans
    37

    bash grep csv dilemea

    Hi guys / Gals

    i have a csv file as below and i would like to use a linux command grep/awk ??? to add a comma before the date so it give me another column in the csv

    HTML Code:
    daryl 1985,09:00,11:05,Movie Mix
    a town-called-panic-2009,11:00,12:30,Film4
    the-hunchback-of-notre-dame-1982,11:05,13:10,Movie Mix
    the-truman-show-1998,11:25,13:25,More4
    carry-on-up-the-khyber-1968,12:30,14:15,Film4
    the-quiet-man-1952,12:35,15:10,Channel 4
    to-walk-with-lions-1999,13:10,15:15,Movie Mix
    double-wedding-2010,13:15,15:00,Five USA
    the-simpsons-movie-2007,14:15,16:00,Film4
    far-from-home-the-adventures-of-yellow-dog-1994,15:15,17:00,Movie Mix

    so the first and every other line should read

    daryl ,1985,09:00,11:05,Movie Mix

    any help would be appreciated

  2. #2
    Join Date
    Apr 2012
    Beans
    7,126

    Re: bash grep csv dilemea

    What exactly is the format? Your first line shows a space between the title and the four digit year field, whereas the rest have a hyphen separator

    You could do something like

    Code:
    $ sed -r 's/(.)([0-9]{4}),([0-9]{2}:[0-9]{2}),([0-9]{2}:[0-9]{2})/,\2,\3,\4/' yourfile
    which matches any single character (separator) followed by yyyy,hh:mm,hh:mm and replaces the first single character with a comma

    Code:
    $ sed -r 's/(.)([0-9]{4}),([0-9]{2}:[0-9]{2}),([0-9]{2}:[0-9]{2})/,\2,\3,\4/' movies.csv
    daryl,1985,09:00,11:05,Movie Mix
    a town-called-panic,2009,11:00,12:30,Film4
    the-hunchback-of-notre-dame,1982,11:05,13:10,Movie Mix
    the-truman-show,1998,11:25,13:25,More4
    carry-on-up-the-khyber,1968,12:30,14:15,Film4
    the-quiet-man,1952,12:35,15:10,Channel 4
    to-walk-with-lions,1999,13:10,15:15,Movie Mix
    double-wedding,2010,13:15,15:00,Five USA
    the-simpsons-movie,2007,14:15,16:00,Film4
    far-from-home-the-adventures-of-yellow-dog,1994,15:15,17:00,Movie Mix

  3. #3
    Join Date
    Aug 2013
    Beans
    37

    Re: bash grep csv dilemea

    thanks that worked a treat just what i needed !

  4. #4
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: bash grep csv dilemea

    That will also match the perl regular expression:

    Code:
    perl -pe 's/^(.*\w+[- ])+(\d{4}.*)$/$1,$2/g' csv.txt
    Not sure if it's the best expression, but this was a nice bit for me to practice on!

  5. #5
    Join Date
    Dec 2007
    Beans
    12,133

    Re: bash grep csv dilemea

    I came up with:
    Code:
    sed -r 's/( |-)([0-9]{4})/,\2/' movies.csv
    Code:
    [11:20 AM] ~/Desktop $ sed -r 's/( |-)([0-9]{4})/,\2/' movies.csv
    daryl,1985,09:00,11:05,Movie Mix
    a town-called-panic,2009,11:00,12:30,Film4
    the-hunchback-of-notre-dame,1982,11:05,13:10,Movie Mix
    the-truman-show,1998,11:25,13:25,More4
    carry-on-up-the-khyber,1968,12:30,14:15,Film4
    the-quiet-man,1952,12:35,15:10,Channel 4
    to-walk-with-lions,1999,13:10,15:15,Movie Mix
    double-wedding,2010,13:15,15:00,Five USA
    the-simpsons-movie,2007,14:15,16:00,Film4
    far-from-home-the-adventures-of-yellow-dog,1994,15:15,17:00,Movie Mix
    [11:21 AM] ~/Desktop $

  6. #6
    Join Date
    Apr 2012
    Beans
    7,126

    Re: bash grep csv dilemea

    ^^^ that's fine, only reason I added more groups (to make the match more specific)

    Code:
    ,([0-9]{2}:[0-9]{2}),([0-9]{2}:[0-9]{2})
    was in case there are movie titles that have 'yyyy' years in them

  7. #7
    Join Date
    Dec 2007
    Beans
    12,133

    Re: bash grep csv dilemea

    Quote Originally Posted by steeldriver View Post
    ...
    was in case there are movie titles that have 'yyyy' years in them
    But won't that be needed only if we do 's/find/replace/g'? Otherwise, won't just the leftmost find/replace on each line be done?

  8. #8
    Join Date
    Apr 2012
    Beans
    7,126

    Re: bash grep csv dilemea

    Quote Originally Posted by vasa1 View Post
    But won't that be needed only if we do 's/find/replace/g'? Otherwise, won't just the leftmost find/replace on each line be done?
    the movie title *is* the leftmost field

  9. #9
    Join Date
    Dec 2007
    Beans
    12,133

    Re: bash grep csv dilemea

    Quote Originally Posted by steeldriver View Post
    the movie title *is* the leftmost field
    True!

  10. #10
    Join Date
    Dec 2007
    Beans
    12,133

    Re: bash grep csv dilemea

    How about
    Code:
    sed -r 's/( |-)([0-9]{4},)/,\2/' movies.csv
    It works on this:
    Code:
    daryl 1985,09:00,11:05,Movie Mix
    a town-called-panic-2009,11:00,12:30,Film4
    the-1954 appearance of the hunchback-of-notre-dame-1982,11:05,13:10,Movie Mix
    the-truman-show-1998,11:25,13:25,More4
    carry-on-to-2014-1968,12:30,14:15,Film4
    the-quiet-man-1952,12:35,15:10,Channel 4
    to-walk-with-lions-1999,13:10,15:15,Movie Mix
    double-wedding-2010,13:15,15:00,Five USA
    the-simpsons-movie-2007,14:15,16:00,Film4
    far-from-home-the-adventures-of-yellow-dog-1994,15:15,17:00,Movie Mix
    in which the 3rd and fifth lines have the year in the title. It assumes that the titles won't have a comma in them.

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
  •