简体   繁体   English

在文件夹中分割文本文件

[英]Splitting text files in folder

I have been trying to make a shell script that will split text files one after the other through an entire folder and deposit every split chunk into another designated folder. 我一直在尝试制作一个Shell脚本,该脚本将一个文本文件通过整个文件夹一个接一个地拆分,并将每个拆分的块存放到另一个指定的文件夹中。

Here is what I have so far, I know its probably clunky(have never tried writing a .sh before): 这是到目前为止,我知道它可能很笨拙(以前从未尝试编写.sh):

#!/bin/bash
#File Split Automation

echo "Usage: split [Folder w/ Input] [Folder For Outputs] [Options] [PREFIX]
  Options: -b [sizeMB]: Split by size
  -l [No. of Lines]: Split by Lines
  If No Output Folder is Defined Default is Set To: /Desktop/splitter-parts
  If No Options Are Selected Default is Size=100MB"

inputdirc=$1
outputdirc=$2
spltion=$3
meastick=$4
prefixture=$5

if [ -d $1 ]
then
    echo "You Picked The Folder $1 To Split Files From"
    ls $1
else
    exit
fi

if [ -d $2 ]
then
    echo "Please Confirm Folder Path For Output $outputdirc"
else
    cd /root/Desktop/
    mkdir -p splitter-parts
fi



read -t 10 -p "Press Enter Or Wait 5 Sec. To Continue"


cd $2

for swordfile in $( ls $1); 
do
command -p split $3 $4 -a 3 -d $swordfile $5

done

Anything you see going wrong? 您发现有什么问题吗? Because I am not getting the output I desired, though it functioned fine when I just had a file and a folder in the split-command string. 因为我没有得到我想要的输出,但是当我在分割命令字符串中只有一个文件和一个文件夹时,它可以正常工作。

EDIT:::: 编辑::::

Sorry, I apologize. 对不起,我很抱歉。 Just getting a bit ahead of myself. 只是有点超越自己。

This is what I am seeing when I run it: 这是我运行它时看到的:

root@kali:~/Desktop/Wordlists# ./splitter.sh '/root/Desktop/Wordlists'               '   /root/Desktop/Untitled Folder' s 100MB
Usage: split [Folder w/ Input] [Folder For Outputs] [Options] [PREFIX]
Options: -b [sizeMB]: Split by size
-l [No. of Lines]: Split by Lines
If No Output Folder is Defined Default is Set To: /Desktop/splitter-parts
If No Options Are Selected Default is Size=100MB
You Picked The Folder /root/Desktop/Wordlists To Split Files From
10dig10milup2.txt                     mixed.txt
10dig10miluplow2.txt                      movie-characters.txt
10dig10miluplow3.txt                      name1s.txt
((------------------CUT------------)
lower.lst                         xae2.txt
lower.txt                         xaf2.txt
mangled.lst                       xag2.txt
mangled.txt                       xah6.txt
misc-dictionary.txt
./splitter.sh: line 24: [: /root/Desktop/Untitled: binary operator expected
Press Enter Or Wait 5 Sec. To Continue
./splitter.sh: line 37: cd: /root/Desktop/Untitled: No such file or directory
split: extra operand `10dig10milup2.txt'
Try `split --help' for more information.
split: extra operand `10dig10miluplow2.txt'
Try `split --help' for more information.
split: extra operand `10dig10miluplow3.txt'
Try `split --help' for more information.
split: extra operand `10dig10miluplow4.txt'
Try `split --help' for more information.
...................MORE OF THE SAME.......

As far as what I am supposed to see, I haven't gotten that far yet, clearly I am missing some steps. 就我应该看到的内容而言,我还没有走那么远,很明显,我缺少一些步骤。

A quick rewrite with some notes to follow: 快速重写并附带一些注意事项:

#!/bin/bash
#File Split Automation

usage="Usage: split [Options] [Folder w/ Input] [Folder For Outputs] [PREFIX]
  Options: -b [sizeMB]: Split by size
  -l [No. of Lines]: Split by Lines
  If No Output Folder is Defined Default is Set To: /Desktop/splitter-parts
  If No Options Are Selected Default is Size=100MB"

split_opt="-b 100MB"
while getopts hb:l: opt; do
    case $opt in
        h) echo "$usage"; exit ;;
        b) split_opt="-b $OPTARG" ;;
        l) split_opt="-l $OPTARG" ;;
    esac
done
shift $((OPTIND - 1))

if [[ $# -eq 0 ]]; then
    echo "$usage"
    exit 1
fi

inputdirc=$1
if [[ -d $inputdirc ]]; then
    ls $1
else
    echo "no such directory: $inputdirc" >&2
    exit 1
fi

if [[ -n $2 ]]; then
    outputdirc=$2
else
    outputdirc=/root/Desktop/splitter-parts
fi

prefixture=$3

mkdir -p "$outputdirc"
cd "$outputdirc"

for swordfile in "$inputdirc"/*; do
    command -p split $split_opt -a 3 -d "$swordfile" $prefixture 
done

Notes: 笔记:

  • you generally want to quote all your variables. 您通常希望引用所有变量。 This is the cause of your error, because there was a file with whitespace and square brackets in the name. 这是导致错误的原因,因为名称中包含带空格和方括号的文件。
  • I did not quote a couple in the split command because I specifically want the shell to perform word splitting on the values 我没有在split命令中引用一对,因为我特别希望shell对值执行单词拆分
  • since options are, well, optional, use getopts to collect them. 因为选项是可选的,所以请使用getopts来收集它们。
  • you store the positional parameters in variables, but you continue to use the positional parameters. 您将位置参数存储在变量中,但是继续使用位置参数。 Pick one or the other. 选择一个或另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM