Convert comma separated items into new line

[root@test]# cat input
a,b,c,d
[root@test]# cat input | sed 's/','/\n/g'
a
b
c
d
[root@test]# cat input | awk '$1=$1' RS=, OFS="\n"
a
b
c
d
[root@test]# cat input | tr -s ',' '\n'
a
b
c
d

Enjoy 🙂

Advertisements

Bash scripts to find out missing lines and common lines between two files

Sometime back I had explained how to find the difference and common lines in two files, Using that giving you 2 simple (very simple) scripts

1. Find out the missing lines between two files

cat diff.sh

#!/bin/bash
if [ $# -eq 0 ];then
        echo "No arguemnts given; Usage : ./diff file2 file1"
        exit 1
fi
echo "Lines exist in $2 and not in $1 are :"
grep -Fxv -f $1 $2

2. Find out the common lines between two files

cat common.sh

#!/bin/bash
if [ $# -eq 0 ];then
        echo "No arguments given; Usage : ./common file1 file2"

        exit 1
fi
echo "Common files in $1 and $2 are :"
awk 'NR==FNR{a[$1]++;next} a[$1] ' $1 $2

Enjoy 🙂

Run a script at boot time in CentOS 7

By default /etc/rc.local and /etc/rc.d/rc.local are no longer executable in CentOS7 with the new systemd-changes. Follow the below steps to make the script /root/g.sh run at boot time:

1. chmod +x /etc/rc.d/rc.local
2. chmod +x /root/g.sh
2. Mention your script at the bottom of the file /etc/rc.local (/etc/rc.local is a symlink to /etc/rc.d/rc.local)as below :

sh /root/g.sh

Restart and check 🙂

Get notification after every successful backup from BackupPC

Edit the configuration in BackupPC is as below :

Edit config –> Backup Settings –> User commands -> DumpPostUserCmd as
/theg/email.sh $xferOK $host $type $client $hostIP $share $XferMethod $sshPath $cmdType
where /theg/email.sh is the custom script.

Below are the contents of the email.sh script :

 

#!/bin/bash
# script to send simple email
# Email To ?
EMAIL=”your_email_address”
# Email text/message
EMAILMESSAGE=”/tmp/emailmessage.txt”

# Grab the status variables
xferOK=$1
host=$2
type=$3
client=$4
hostIP=$5
share=$6
XferMethod=$7
sshPath=$8
cmdType=$9

# Check if backup succeeded or not.
if [[ $xferOK == 1 ]]; then
        STATUS=”SUCCESS”
else
        STATUS=”FAILED”
fi

# email subject
SUBJECT=”[BackupPC] $STATUS for host: $client”

# Email text/message
echo “Filesystem backup for $host $STATUS” > $EMAILMESSAGE
echo “” >>$EMAILMESSAGE
echo “Type: $type” >>$EMAILMESSAGE
echo “Client: $client” >>$EMAILMESSAGE
echo “Host: $host” >>$EMAILMESSAGE
echo “Host IP: $hostIP” >>$EMAILMESSAGE
echo “Share: $share” >>$EMAILMESSAGE
echo “XferMethod: $XferMethod” >>$EMAILMESSAGE
echo “sshPath: $sshPath” >>$EMAILMESSAGE
echo “cmdType: $cmdType” >>$EMAILMESSAGE

# send an email using /bin/mail
/bin/mail -s “$SUBJECT” “$EMAIL” < $EMAILMESSAGE

Enjoy 🙂