Find the answer to your Linux question:
Results 1 to 4 of 4
I'm having a hell of a time writing a script in which I would like to obtain two variables from the current directory. I'm trying to parse the season and ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    8

    regexp with sed help

    I'm having a hell of a time writing a script in which I would like to obtain two variables from the current directory.

    I'm trying to parse the season and episode number from the directory name, ie
    "Top Gear 14x01 - blah"
    and
    "Top Gear 5x08 - blah"
    but I can't come up with a common regexp set for sed that will get me the season (14 or 5) and episode (01 or 0 consistently. I either get 4 and 5 or 14 and nothing for the season.

    Any help with figuring this out is much appreciated!
    thanks!

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    you can do it with the shell
    Code:
    $ file="Top Gear 14x01 - blah"
    $ set -- $file
    $ echo $3
    14x01
    $ t=$3
    $ IFS="x"
    $ set -- $t
    $ echo $1
    14
    $ echo $2
    01
    $ unset IFS

  3. #3
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    506
    How about this?
    Code:
    #!/bin/bash
    DIR="Top Gear 5x08 - blah"
    SEASON="$(echo "$DIR" | sed 's/.* \([0-9]\+\)x[0-9]\+ .*/\1/')"
    EPISODE="$(echo "$DIR" | sed 's/.* [0-9]\+x\([0-9]\+\) .*/\1/')"

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    It would be even easier if you use perl:
    Code:
    $ echo "Top Gear 5x08 - bla"| perl -e 'while (<>) {next if !/(\d+)x(\d+)/; print("season=$1;episode=$2\n");}'

Posting Permissions

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