0

In my script I am taking a text file and splitting into sections. Before doing any splitting, I am reformatting the name of the text file. PROBLEM: Creating a folder/directory and naming it the formatted file name. This is where segments are placed. However the script breaks when the text file has spaces in it. But that is the reason I am trying to reformat the name first and then do the rest of the operations. How could I do so in that sequence?

execute script: text_split.sh -s "my File .txt" -c 2

text_split.sh

# remove whitespace and format file name
FILE_PATH="/archive/"
find $FILE_PATH -type f -exec bash -c 'mv "$1" "$(echo "$1" \
| sed -re '\''s/^([^-]*)-\s*([^\.]*)/\L\1\E-\2/'\'' -e '\''s/ /_/g'\'' -e '\''s/_-/-/g'\'')"' - {} \;
sleep 1

# arg1: path to input file / source
# create directory 
function fallback_out_file_format() {
     __FILE_NAME=`rev <<< "$1" | cut -d"." -f2- | rev`
     __FILE_EXT=`rev <<< "$1" | cut -d"." -f1 | rev`
     mkdir -p $FILE_PATH${__FILE_NAME};
     __OUT_FILE_FORMAT="$FILE_PATH${__FILE_NAME}"/"${__FILE_NAME}-part-%03d.${__FILE_EXT}"
     echo $__OUT_FILE_FORMAT
     exit 1 
}

# Set variables and default values
OUT_FILE_FORMAT=''

# Grab input arguments
while getopts “s:c” OPTION
do
     case $OPTION in
         s) SOURCE=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;;
         c) CHUNK_LEN="$OPTARG" ;;
         ?) usage
            exit 1
            ;;
     esac
done

if [ -z "$OUT_FILE_FORMAT" ] ; then
    OUT_FILE_FORMAT=$(fallback_out_file_format $SOURCE)
fi
Code_Ed_Student
  • 1,180
  • 6
  • 27
  • 67
  • I don't see any directory creation in the code, so I wonder if the title of your question is more about your final goal than it is about your current obstacle. Can you adjust the title, question, and code sample to focus on the problem you're trying to get help with? (For instance, is it the long find command?) – Nick Russo Oct 07 '14 at 03:45
  • @NickRusso I forgot to append that part. I did now. The issue is that the script does in the first few lines perform a file name format but it still uses the old file name - this runs into a problem when creating a directory because the old file name had spaces – Code_Ed_Student Oct 07 '14 at 03:49

3 Answers3

0

You can trim whitespace from the input string using tr -d ' '.

You can then join this to your FILE_PATH variable with something like this:

FILE_NAME=$(echo $1 | tr -d ' ')
FILE_PATH="/archive/"
FILE_PATH=$FILE_PATH$FILE_NAME
Travis
  • 2,579
  • 18
  • 19
0

Your script takes a filename argument, specified with -s, then modifies a hard-coded directory by renaming the files it contains, then uses the initial filename to generate an output directory and filename. It definitely sounds like the workflow should be adjusted. For instance, instead of trying to correct all the bad filenames in /archive/, just fix the name of the file specified with -s.

To get filename and extension, use bash's string manipulation ability, as shown in this question:

filename="${fullfile##*/}"
extension="${filename##*.}"
name="${filename%.*}"
Community
  • 1
  • 1
Nick Russo
  • 1,522
  • 10
  • 13
  • Well my final task is to remove the `-s` from the script call and have it just point at the directory and loop through all files and slice them. – Code_Ed_Student Oct 07 '14 at 04:01
  • It's probably best to stick with one file, or all files, but not mix the two. Just one file is probably easiest for the moment. Then you can create a separate script that uses find and invokes your first script on each applicable file. – Nick Russo Oct 07 '14 at 04:03
0

You can escape the space using a back slash \ Now the user may not always provide with the back slash, so the script can use sed to convert all (space) to \

sed 's/ /\ /g'

you can obtain the new directory name as

dir_name=`echo $1 | sed 's/ /\ /g'
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52