Monday, February 29, 2016

sed: Using Positional Groupings with \( \) and \1,\2

Use \( \) to save part of a regular expression and \1 and  \2 to recall them.

bash-4.2# cat test_h
1..........5
5.........10
10........20
100......200

bash-4.2# sed -e 's/\([0-9][0-9]*\)\.\{5,\}\.\([0-9][0-9]*\)/\1-\2/' test_h


1-5
5-10
10-20
100-200

Saturday, February 20, 2016

Bash shell script: back up production server

#!/bin/bash

#back up production on development server

# options added 11.23.2015

#written by Ken Sipe

# variables
today=$(date +%y%m%d)
c_source=/sever2-cgi-bin
d_source=/sever2-docs
t_source=/server2-tid
c_target=/usr2/cgi-bin
d_target=/usr2/docs
t_target=/usr3/docs/tid

if [ $EUID -eq 0 ]
then
                while getopts acdht opt
        do
                case "$opt" in
                    h)
                        # help

                        echo "Options:"
                        echo "-a. Backup all webserver files (e.g. cgi-bin, docs, tid directories)"
                        echo "-c. Backup the cgi-bin directory only"
                        echo "-d. Backup the docs directory only"
                        echo "-t. Backup the tid directory only"
                        echo "Note: You can select more than one option (e.g. -ct or -d -c)"
                        echo ;;
                    a)
                        #cgi-bin routine

                        if cd $c_source
                        then
                                tar -cvf - . | (cd $c_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin_t3p.$today
                        else
                                echo "Directory $c_source does not exist"
                        fi

                        #docs routine

                        if cd $d_source
                        then
                                tar -cvf - . | (cd $d_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_docs_t3p.$today
                        else
                                echo "Directory $d_source does not exist"
                        fi

                        #tid routine

                        if cd $t_source
                        then
                                tar -cvf - . | (cd $t_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_tid_t3p.$today
                        else
                                echo "Directory $t_source does not exist"
                        fi

                        #list results

                        echo "Directory $c_target log saved"
                        ls -lR $c_target > /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin.$today

                        echo "Directory $d_target log saved"
                        ls -lR $d_target > /homeb/d54712/usr-scripts-save/backup/logs/log_docs.$today

                        echo "Directory $t_target log saved"
                        ls -lR $t_target > /homeb/d54712/usr-scripts-save/backup/logs/log_tid.$today ;;
                    c)

                        #cgi-bin routine

                        if cd $c_source
                        then
                                tar -cvf - . | (cd $c_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin_t3p.$today
                        else
                                echo "Directory $c_source does not exist"
                        fi

                        #list results

                        echo "Directory $c_target log saved"
                        ls -lR $c_target > /homeb/d54712/usr-scripts-save/backup/logs/log_cgi-bin.$today ;;

                    d)

                        #docs routine

                        if cd $d_source
                        then
                                tar -cvf - . | (cd $d_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_docs_t3p.$today
                        else
                                echo "Directory $d_source does not exist"
                        fi

                        #list results

                        echo "Directory $d_target log saved"
                        ls -lR $d_target > /homeb/d54712/usr-scripts-save/backup/logs/log_docs.$today ;;

                    t)

                        #tid routine

                        if cd $t_source
                        then
                                tar -cvf - . | (cd $t_target; tar -xvf - ) | tee  /homeb/d54712/usr-scripts-save/backup/logs/log_tid_t3p.$today
                        else
                                echo "Directory $t_source does not exist"
                        fi

                        #list results

                        echo "Directory $t_target log saved"
                        ls -lR $t_target > /homeb/d54712/usr-scripts-save/backup/logs/log_tid.$today ;;

                esac
        done
else
        echo "This script must be run as root"
        echo "User is $USER"
fi

Wednesday, February 17, 2016

Linux: Proper use of xargs

Credit: http://www.humans-enabled.com/2012/06/how-to-use-xargs-to-grep-or-rm-million.html


A CORRECT way to use xargs with grep:


$ find . -type f -print0 | xargs -0 grep 'rubies'


./file-78432:rubies diamonds and gold

In the above example, the find command checks the current directory for files of type and formats the output, replacing blank spaces in names with the null character (-print0), which then gets piped to the xargs command. The xargs command accepts the output from the find command, while ensuring no blank spaces with the -0 (format of -print0 command required), and greps the results for 'rubies'. As you can see in the output, this is how it's supposed to work.




The WRONG way to use xargs with grep


$ find . -type f | xargs -0 grep 'rubies'

xargs: argument line too long
In the above example, when the find command encounters our filename with 3 spaces in it, they are piped into the xargs command as 3 arguments at once, which causes an error because our xargs command only expects 1 argument.

Sunday, February 14, 2016

Sed string substitution over multiple files


find ./ -type f -exec sed -i 's/string1/string2/g' {} \;


or using xargs...


find ./ -type f -print0 | xargs -0 sed -i 's/string1/string2/g'