On 11/17/2012 1:50 PM, Tim Chase wrote:
On 11/16/12 18:36, Ant wrote:
this one is something I need help in. Say I have thousands of
files and I wanted to concatenate them all into one. THis is easy
to do, I can just do it outside Vim using bash script "cat * >
allfiles.txt" or something.

But how would you concatenate heaps of files together, separating
them by its filename or some unique thing
You allude to how you get the source of your filenames ("*" = all
files in the current directory), so to do similarly in Vim, I might
do something like

   :enew
   :r! /bin/ls  " on *nix
   :r! dir /b   " on Win32
   :%sort       " optionally sort, as dir/ls may not return sorted
                " though you could pass switches to ls/dir to sort
   :1d          " delete the first blank line
   :g/^/-put=repeat('=',50)|+|exec 'read '.getline('.')
                " insert a divider of 50 "=" characters,
                " move back to the line with the filename,
                " read the file named on the given line

This also has the advantage that, if you have the filenames in a
file already, you can just use that, instead of reading the
filenames into the file contents.  In theory, the above should also
work with subdirectories too if you need.

-tim


try this:
http://code.google.com/p/filemerger/

this is something I produced long time ago, to get done what I needed.
its very basic and need a lot enhancement, but it works and got my work done. it can find specified type of file from a folder (or even a tar.gz ball) and cat them into a big file with(or without) an additional mark for Vim VOom plugin(my favorite).

let me know if you have any questions.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
#!/bin/bash
ver=1.5

#global vars initialization (def values)
#get the file basename
progname=$(basename $0)
filetype='-name "*.txt"'
mark=0          #voom mark button
unix=0          #unix to dos button
filename=1      #button of file type merge
interactive=
verbose=
dir=.
file=all_test.txt
tarball=
ft_tgz=" -or -name \"*.tar.gz\""
ft_zip=" -or -name \"*.zip\""
ft_rar=" -or -name \"*.rar\""

echo -e "$progname version $ver\n,author ping,copyright 2010-2012"

#usage prompt
usage () {
    echo "$progname: usage: $progname [options] DIR FILENAME"
    cat <<- _USAGE_
        [options]
            -t (txt|htm|log|...):type of file you want to merge
            -m: add marks on the matched file names when marge (for vim voom)
            -M: don't append marks
            -u: convert to unix
            -U: don't convert to unix
            -b: add extra empty lines between each files
            -i: interactive mode (not done yet)
            -T: tarball support (.tar.gz,new!)
_USAGE_
    return
}

#options handling
while getopts ":t:mMivuUTh" opt; do
    case $opt in
        t )     filetype="-name \"*$OPTARG\""
                echo -e "get an option: -t $OPTARG"
                echo -e "compose filetype to be:$filetype\n"
                ;;
        T )     tarball=1
                echo -e "tarball option enabled"
                ;;
        m )     mark=1
                echo -e "force marks"
                ;;      
        M )     mark=0
                echo -e "no marks"
                ;;
        u )     unix=1
                echo -e "force unix"
                ;;
        U )     unix=0
                echo -e "no dos-unix conversion"
                ;;
        i )     interactive=1
                #todo
                ;;
        b )     blanks=1
                ;;
        v )     verbose=1
                #todo
                ;;
        h )     usage
                exit
                ;;
        \?)     usage 1>&2
                exit 1
                ;;
    esac
done

#handling other parameters following options
shift $(($OPTIND - 1))

#if [[ -z $filetype || -z $dir || -z $file ]]; then
#    usage >&2
#    exit 1
#fi
            
#if no dir and filename followed,exit
if [[ -z "$@" ]]; then
    usage 1>&2
    exit 1
fi

#dir & filename parsing
dir=$1
file=$2
echo "---dir $dir will be checked, and"
echo "---all files found will be merged into $file"

#rm the old file generated last time
if [ -e $file ]; then
    echo -e "---remove the old file:$file"
    rm $file
fi

format () {
        if [ $unix -eq 1 ]; then
        {
        #convert from dos to unix
        echo -e "convert to unix"
        cat "$afile" | tr -d '\r' > temp.txt 
        mv temp.txt "$afile"
        }
        fi

        if [ $mark -eq 1 ]; then
        {
            #mark for Voom
            echo -e "\n\n$afile {{{1\n\n" >> $file
            echo "append Voom marks"
        }
        fi
}


findamerge () {
    dir=$1 
    filetype=$2
    echo "---dir got is $dir"
    echo "---filetype got is $filetype"
    tofind="find \"$dir\" -type f $filetype"
    echo -e "---the following find operation will be executed to find the 
desired files: $tofind\n"

    eval $tofind  | xargs -0 ls  | 
    while read afile;do
        echo "get a file:$afile"
        #file type handling
        case $afile in
            *.htm?)     
               #if it's html
               format
               echo -e "render the webpage"
               #render to text by w3m 
               cat "afile" | w3m -T text/html -dump >> $file ;;

            *txt|*.log|*.cfg)
               #otherwise,just cat it 
               format
               echo -e "cat the plain txt files"
               cat "$afile" >> $file 
               echo -e "\n======================\n" >> $file
               ;;

            *rar)
                #todo
              ;; 

            *zip)
                #todo
              ;;

            *.tar.gz)
                #for tar.gz ball,1st to check out files per file type, 
                #if there is match then extract them out and merge,recursively
                if [ $tarball -eq 1 ]; then
                {
                    n=`IFS=$'\n';tar tf $afile | grep txt | wc -l`
                    if [[ $n -gt 0  ]]; then
                    { #if match found,handle them
                        tmpdir4targz=$(basename $afile)
                        echo -e "tmp dir name is $tmpdir4targz after basename"
                        tmpdir4targz=${tmpdir4targz%.*.*}
                        echo -e "tmp dir name is $tmpdir4targz after trunc"
                        tmpdir4targz="/tmp/logtool.tmp/$tmpdir4targz/"

                        #create a temp dir
                        if [ ! -e $tmpdir4targz ]; then
                        {
                             echo -e "temp dir $tmpdir4targz does not exist, 
create one"
                             mkdir -p $tmpdir4targz
                        }
                        else
                        {
                            echo -e "temp dir $tmpdir4targz existed!"
                        }
                        fi

                        #and untar everything
                        tar zxf $afile -C $tmpdir4targz/ 
                        findamerge "$tmpdir4targz" "$filetype"
                    }
                    else
                    {
                        echo -e "no matching files found inside this tarball!"
                    }
                    fi
                }
                fi
                ;;

            *)
               echo -e "don't do anything for other types\n"
               ;;
        esac
    done

}

filetype="$filetype -print0 $ft_tgz -print0"
findamerge "$dir" "$filetype" 

Reply via email to