Results 1 to 5 of 5

Thread: Shell scripting - duplicate text

  1. #1
    Join Date
    Mar 2007
    Location
    Maine
    Beans
    97
    Distro
    Kubuntu 10.04 Lucid Lynx

    Shell scripting - duplicate text

    I'm trying to find a simple way to move all of my music into subdirectories.
    Step 1 - make the directories.
    I did this with the following 1 liner:
    Code:
    ls -ld * | grep -v drwx | awk '{print $8, $9, $10, $11}' | grep '-' | cut -d'-' -f1 | uniq -i > make_dirs.sh
    I then used some simple search routines in vi to have this listing create the directories:
    Code:
    %s/^/mkdir "/g
    %s/$/"/g
    That way, for every band there is an entry:
    Code:
    mkdir "Band Name"
    Step 2 - move files into directories.
    This is where I run into trouble. I am trying to do something similar. I have again output the same bandnames to a file, and am trying to find a way to double the text on each line:

    Band Name -> Band Name Band Name

    The final goal is:

    Code:
    mv "Band Name"* "Band Name"/.
    Effectively moving all files into the folders.

    I thought I would start in vi with the following search/replace:

    %s/$/"* "/g

    Giving me:
    Band Name"* "

    I then thought I might be able to use cut to duplicate:

    cat make_dirs.sh | cut -d"\"" -f1,2,3,1

    printing the
    1. Band Name
    2. "*
    3. "
    1. Band Name

    But unfortunately, it doesn't repeat printing the first field.

    Any ideas?
    Ubuntu 8.04 Hardy Heron
    BIOSTAR TFORCE 550 Socket AM2 NVIDIA nForce 550 MCP ATX AMD MOBO, AMD Athlon 64 X2 3600+ w/ Arctic Cooling Alpine 64, 3x512MB DDR2 800 OCZ Gold SDRAM, Gigabyte GeForce 7300GT 256MB GDDR2, 160GB SATA 120GB PATA

  2. #2
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Shell scripting - duplicate text

    Code:
    ls -ld * | grep -v drwx | awk '{print $8, $9, $10, $11}' | grep '-' | cut -d'-' -f1 | uniq -i | sed 's/.*/mv \"&\"* \"&\"\/./'

  3. #3
    Join Date
    Mar 2007
    Location
    Maine
    Beans
    97
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Shell scripting - duplicate text

    Whoa - now that right there is some sed action.

    Thanks!

    NOTE:

    Once outputting this to a file, I had to remove some spacing issues:

    %s/\ \"\/\./"\/\./g

    Spaces after the filename of the directory prevented it from moving.

    This also caused some files to get renamed funny... but other than that worked like a charm.
    Ubuntu 8.04 Hardy Heron
    BIOSTAR TFORCE 550 Socket AM2 NVIDIA nForce 550 MCP ATX AMD MOBO, AMD Athlon 64 X2 3600+ w/ Arctic Cooling Alpine 64, 3x512MB DDR2 800 OCZ Gold SDRAM, Gigabyte GeForce 7300GT 256MB GDDR2, 160GB SATA 120GB PATA

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Shell scripting - duplicate text

    you can do that all in awk
    Code:
    find /fullpath -maxdepth 1 -type f  -printf "%f\n"| awk 'BEGIN{
     q="\047"
     FS="-"
    }
    /-/{
      unique[$1] 
      files[$0]=$1
    }
    END {
      #make directories
      for ( dir in unique) {
        cmd = "mkdir -p "q dir q
        # system(cmd) #uncomment to use
      }
      for( f in files ){
       print "moving files in  "f" to "files[f]
       cmd = "mv "q f q"* "q files[f] q
       # system(cmd) # uncomment to use  
      }  
    }'

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Shell scripting - duplicate text

    Quote Originally Posted by omrsafetyo View Post
    Code:
    ls -ld * | grep -v drwx | awk '{print $8, $9, $10, $11}' | grep '-' | cut -d'-' -f1 | uniq -i > make_dirs.sh
    by using awk with specified columns to get the file names, you are prone to to getting date fields as well as file names with spaces. Your result will have errors. Use find instead of ls -l.

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
  •