Schlagwort-Archive: shell

awk script to remove objects from a pdf

First uncompress the pdf if it is compressed with pdftk:
pdftk myfile.pdf output unc.pdf uncompress

Then remove all objects that contain the keywords PDF-XChange|pdfxviewer.com|PDFXCViewer20|Click to buy NOW:

awk '
    BEGIN {
        found=0
    }
    {
    if ( $0 ~ /^[0-9 ]+obj/ ) {
        objectFound=1;
        objectLineCounter=0;
        objektZeilen[objectLineCounter]=$0;
        objectLineCounter++;
    } else if (objectFound == 1) {
        objektZeilen[objectLineCounter]=$0;
        if ( $0 ~ /PDF-XChange|pdfxviewer.com|PDFXCViewer20|Click to buy NOW/ ) {
            found=1;
        }
        if ( ( $0 ~ /endobj/ ) && ( found == 0 ) ) {
            for (i=0; i<length(objektZeilen); i++) {
                print objektZeilen[i];
            }
            delete objektZeilen;
            objectFound=0;
            found=0;
        }
        if ( ( $0 ~ /endobj/ ) && ( found == 1 ) ) {
            delete objektZeilen;
            objectFound=0;
            found=0;
        }
        objectLineCounter++;
    } else {
        print $0
    }
    }
' unc.pdf > test.pdf

Recompress and repair pdf with pdftk:
pdftk test.pdf output comp.pdf compress

Too bad it also removed the OCR layer. I couldn’t find out which layer is responsible for the OCR.

Raspberry Pi reset wireless lan to solve problems with no connection

I found a script on http://askubuntu.com/a/593589 which tried to reset the wlan interface in various ways. I modified that script a little with and installed in on cron and let it execute each minute (*/1 * * * * /root/bin/resetWifi.sh)

resetWifi.sh:


#!/bin/bash
# program to check wifi and reset if not running
if [[ ! -z "$(ps waux | grep $0)" ]]; then exit 0; fi
exec 2>&1 1> >(tee -a $HOME/wificheck.log)
GATEWAY=192.168.1.1
IWCONFIG_BIN=$(which iwconfig)
RFKILL_BIN=$(which rfkill)
PING_BIN=$(which ping)
DEVICE=$(iwconfig 2>/dev/null | grep 802 | awk '{print $1}')
function isPingWorking {
        if ${PING_BIN} -c 1 ${GATEWAY} >/dev/null 2>&1 ; then
                exit 0
        else
                echo "didn't work :("
        fi
}
while true; do
        isPingWorking
        # Failed, try to reset wifi - sometimes works ok
        date
        ifdown ${DEVICE}
        sleep 1
        ifup ${DEVICE}
        sleep 10
        isPingWorking
        echo "turn wlan stick power off... "
        ${IWCONFIG_BIN} ${DEVICE} txpower off
        sleep 3
        ${IWCONFIG_BIN} ${DEVICE} txpower auto
        isPingWorking
        echo "use rfkill to reenable wlan stick... "
        ${RFKILL_BIN} list
        ${RFKILL_BIN} block wifi
        sleep 3
        ${RFKILL_BIN} unblock wifi
        isPingWorking
done
exit 0

Test if you have rfkill installed. The other bins should be available on most default installations.

Film aus einzelne Filmsegmente runterladen und zusammenführen von Arte

  • .m3u8 Datei finden und herunterladen
  • überflüssige Informationen entfernen
    sed -i -e '/^#/d' -e '/^$/d' index_1_av.m3u8
  • parallel die Dateien herunterladen
    cat index_1_av.m3u8 | parallel --gnu "wget -c {}"
  • Dateien umbenennen, damit in der richtigen Reihenfolge zusammengefügt wird
    for i in *.ts; do extracted_number=$(sed -re 's/segment([0-9]*)_.*/\1/' <<<"$i"); mv "$i" "segment_$(printf "%04d" ${extracted_number}).ts"; done
  • zu einem Film zusammenfügen
    cat segment_* > film.ts

Verschiedene Webradios nach x Tagen durchwechseln

Da ich gerne auch lokal die Musik habe, die auf dem von mir gekauften Radiostream läuft lass ich ein Skript laufen, dass nach 2 Tagen rippen zur nächsten Station wechselt und wiederum 2 Tage rippt. Sind alle Stationen durch wird von vorne begonnen. Wird dem Skript als Argument eines der Stichworte für ein bestimmtes Radio übergeben, fängt das rippen bei diesem Radio an.

Hier das Skript:

<pre>#!/bin/bash
DI_FM_FOLDER=/path/to/folder

if [ ! -z "$1" ]; then
        STREAM_TO_PLAY=$1
fi

function startRipping {
        # kill the running streamripper
        killall streamripper
        # lets safe the unix epoch time when the recording starts
        # we use that for calculating how long the webradio recording is running
        STARTTIME=`date +%s`
        echo "currently ripping $1"
        $DI_FM_FOLDER/$1.sh
        # as long as the startdate is greater than the current date minus 2 days the stream is executed
        while true; do
                if [ ! $STARTTIME -ge $(date +%s -d "-2 days") ];then
                        break;
                fi
                sleep 1
        done
}

# if the for loop inside the while exits, it starts again at the beginning (aka endless loop)
while true; do
        # process all the streams 2 days each
        for i in liquid progressive techno minimal psychill chillout trance goa; do
                # if this script is started with an argument, the var $STREAM_TO_PLAY is not empty and it will start
                # the ripping cicle with that stream and skips the ones before it. if it's started at the right point
                # it will set the $STREAM_TO_PLAY far to a empty string and normal ripping can proceed.
                if [ ! -z $STREAM_TO_PLAY ]; then
                        if [ $i == $STREAM_TO_PLAY ];then
                                startRipping $i
                                unset STREAM_TO_PLAY
                        fi
                else
                        startRipping $i
                fi
        done
done</pre>

Im startRipping ist dann nur eine case Kontrollstruktur für jeden einzelnen Radiostream und die jeweilige Radio-IP.