shell to concatenate files

0

Good day, I need to implement a shell which is able to concatenate several .txt files in a single file as if it were a unique script in this file. In addition, you must identify when a new txt is added and be able to concatenate it in the same file. What I have is this:

cat *.txt > temperaturas.txt 	
    
asked by Paula Marín 12.11.2018 в 23:30
source

3 answers

1

you can do it like this:

    #!/bin/bash

    #carpeta y archivo de concatenaciones
    DIRECTORY="./html/"
    OUTPUT="./output.txt"
    IGNORE="PDF|pdf|exe|EXE"

    #archivo de estado
    FILES="./files.txt"

    #hacemos un for en base a los archivos en la carpeta
    for i in $(/bin/ls $DIRECTORY | grep -Ev $IGNORE);
    do
     # validamos que sea un archivo y sea legible
     if [[ -r DIRECTORY/$i ]]; then # puede ser cambiado -r por -f solo para que valide que es un archivo
        #validamos que el archivo de resultados exista!
         if [[ ! -f $FILES ]]; then
            # si no existe se le concatena el primer resultado y se le agrega a la lista
echo "[+] el archivo $i se va a concatenar a $OUTPUT";
             echo $i >> $FILES;
             cat $i >> $OUTPUT;
         fi
        # validamos si los archivos en carpeta existan en el documento de archivos listados
        # si no existe lo agregamos
         if [[ $(cat $FILES | grep $i | grep -v "grep" )  == "" ]]; then
            echo "[+] el archivo $i se va a concatenar a $OUTPUT";
            # agregamos el nuevo archivo a nuestra lista
            echo DIRECTORY/$i >> $FILES
            # agregamos el nuevo contendido al archivo de salida
            cat DIRECTORY/$i >> $OUTPUT
         fi
     else
        echo "[-] El archivo $i no se puede leer"
     fi
    done
    
answered by 13.11.2018 / 20:11
source
4

You can put a file with a while and perform the concatenation.

Something for this style

#!/bin/bash

RUTA="./ruta"  # Esta es una carpeta donde estarán los archivos a concatenar.
ARCHIVO_CONDENSADO="./archivo_condensado"

INTERVALO="3s" #intervalo de tiempo en el formato que acepta el comando sleep

while :
do
    cat "$RUTA"/*.txt > "$ARCHIVO_CONDENSADO"
    sleep "$INTERVALO"
done

And out of the script, and giving you the execution permission, you just run.

$ nohup ./script &

And the files will be concatenated to one every 3 seconds on writing the file "condensed_file" which is where all the concatenated files will be stored. Although it is very short time has the "potential" that, unlike a cronjob, can be run in fractions of minutes, since a cronjob has the minute as a minimum unit. In that script you can change the value of the interval variable following the format requested by the command sleep (you can check in man sleep ) The problem with my script is that, since my few years as a developer, I do not see any meaning to its existence haha;) unless it is a personal exercise. This could be replaced by an instruction from a cronjob.

$ crontab -e

Then it will ask you to edit a file and put something of this style:

* * * * * cat /ruta/a/tu/carpeta/*.txt > /ruta/a/tu/archivo

You save that file that makes you edit the crontab -e command and with that a task is installed that will be executed every minute which will consist in concatenating all the files from one folder to another file every minute, every day of every month every year. Although that does not make sense either because you concatenate ... And then what ?, Maybe you prefer to make a backup with tar and, every so often, upload it to a database or store it on the server or something more than about write files of one kind or another.

If you want something that checks and operates according to changes in the contents of the folder or files, you would have to create a journaling class. And if, with these two options previously raised, is not enough, do not hesitate to comment.

Note : I have not run any of the options, I wrote it on the flight so feel free to comment if it did not work.

    
answered by 13.11.2018 в 18:41
0

You can do it using type :

type "C:\<Directorio conteniendo archivos>\*.txt" > temperaturas.txt 

in this way the contents of all the files in the directory will be appended to the file temperaturas.txt .

    
answered by 13.11.2018 в 00:15