string_out: remova a primeira ocorrência de uma string em um arquivo de texto

Publicado por Luís Fernando C. Cavalheiro (última atualização em 25/04/2016)

[ Hits: 2.208 ]

Homepage: https://github.com/lcavalheiro/

Download string_out




Cambada de clicadores, aqui começa mais um Script do Dino® trazendo para todos uma ferramenta útil ou curiosa. Hoje temos o string_out, que remove a primeira ocorrência de uma string em um arquivo de texto. Divirtam-se com ela!

  



Esconder código-fonte

#!/bin/bash
#
# ------------------------------- Legal notice -------------------------------
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Tradução não-oficial:
#
# Este programa é um software livre; você pode redistribuí-lo e/ou 
# modificá-lo dentro dos termos da Licença Pública Geral GNU como 
# publicada pela Fundação do Software Livre (FSF); na versão 3 da 
# Licença, ou (na sua opinião) qualquer versão.
#
# Este programa é distribuído na esperança de que possa ser útil, 
# mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO
# a qualquer MERCADO ou APLICAÇÃO EM PARTICULAR. Veja a
# Licença Pública Geral GNU para maiores detalhes.
#
# Você deve ter recebido uma cópia da Licença Pública Geral GNU junto
# com este programa. Se não, veja <http://www.gnu.org/licenses/>.
#
# ------------------------------- Legal notice -------------------------------
#

#  Define help and version funcionts

show_help() {
    cat << EOF

    Copyright 2016 Luís Fernando Carvalho Cavalheiro
    Licence: GPLv3 (run "string_out -l" to read licence abstract)

    string_out
    description: removes first occurrence of string "string" from file "file"
    usage: string_out [ -s "string" -f "file" -c "suffix" ] | [ -h | -v | -l ]

    options:
    -s "string"     defines which string will be removed, quotes required
    -f "file"       defines from which file string will be removed, quotes required
    -c "suffix"     creates a copy of original file attaching "suffix" at its name
    -h              shows this help text
    -v              shows string_out version and changelog
    -l              shows GPLv3 abstract

    If used to remove a string from file, options -s and -f are mandatory. If any of
    -h, -v or -l options are used, they will take precedency over -s and -f and this
    script will use only the last one passed to command line.

EOF
}

show_version() {
    cat << EOF

    Copyright 2016 Luís Fernando Carvalho Cavalheiro
    Licence: GPLv3 (run "string_out -l" to read licence abstract)

    string_out
    description: removes first occurrence of string "string" from file "file"

    string_out version and changelog history:

    0.1 (RJU 20160331): first functional draft.
    1.0 (RJU 20160331): first complete functional version
    1.1 (RJU 20160331): added -c option
    1.2 (RJU 20160331): simplified sed command, thanks to Elder Marco

EOF
}

show_licence() {
    cat << EOF

    Copyright(C) 2016 Luís Fernando Carvalho Cavalheiro
    Licenced under GPLv3.

    string_out
    description: removes first occurrence of string "string" from file "file"

    Generic Public Licence, version 3, abstract:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

EOF
}

set -e

# Main script loop, eval command line arguments

while getopts ":hvls:f:c:" opt ; do
    case "$opt" in
        s)
            ACTION="s"
            STRING="$OPTARG"
            ;;
        f)
            CHECKACTION="s"
            FILE="$OPTARG"
            ;;
        c)
            SUFFIX="$OPTARG"
            ;;
        h)
            ACTION="h"
            ;;
        v)
            ACTION="v"
            ;;
        l)
            ACTION="l"
            ;;
        :)
            echo "Option -$OPTARG requires an argument." >&2
            show_help
            exit 1
            ;;
        \?)
            echo "Invalid option: -$OPTARG." >&2
            show_help
            exit 1
            ;;
    esac
done

# Shows help text if no option was provided on command line

if [ $OPTIND -eq 1 ] ; then
    cat << EOF

    string_out requires at least one argument!
EOF
ACTION="h"
fi

# If processing a file, check if both -s and -f options were provided

if [ "$ACTION" != "$CHECKACTION" ] ; then
    cat << EOF

    You need to provide both -s and -f options with their respective arguments!
EOF
ACTION="h"
fi

# Process command line options

case "$ACTION" in
    h)
        show_help
        ;;
    v)
        show_version
        ;;
    l)
        show_licence
        ;;
    s)
        sed -i"$SUFFIX" -e "0,/$STRING/ { /$STRING/ d }" "$FILE"
        ;;
esac

Scripts recomendados

pdfcon - leitor de PDF em modo texto

mysqlapache2combo - Pequeno script que facilita a vida

Checar o uso da internet

Popcorn-Time via shell script

SlackAcc


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts