Doing bulk file update (mind escapes when using complicated patterns):
#!/bin/bash
if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ]; then
printf "\nPlease enter:\n
1. file command-compatible filename pattern
2. backup suffix for affected files
3. pattern to replace
4. replacement\n\n
For instance, $0 *.txt .xml-backup blue red\n
This should replace blue with red in all txt files\n\n"
exit
fi
filename_pattern=$1
backup_suffix=$2
pattern_to_replace=$3
replace_with=$4
find . -type f -name ${filename_pattern} -exec sed -i${backup_suffix} "s|${pattern_to_replace}|$replace_with|g" {} \+
Friday, June 7, 2019
Bash - Cut a fragment of a log by some criteria (like timestamp)
Here is how to cut out a piece of a log between two unique strings (like timestamps):
#!/bin/bash
printf "\nResults are returned *inclusive*\n\n"
if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
printf "\nPlease enter SUBSTRSTART SUBSTREND and LOGPATH\nFor instance, $0 11:59:17,206 12:02:14,606 test.log\n\n"
exit
fi
substrstart=$1
substrend=$2
logpath=$3
sed -n "/${substrstart}/,/${substrend}/p" ${logpath} | tee log_excerpt-$(date +%F_%H-%M-%S) | cat
#!/bin/bash
printf "\nResults are returned *inclusive*\n\n"
if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
printf "\nPlease enter SUBSTRSTART SUBSTREND and LOGPATH\nFor instance, $0 11:59:17,206 12:02:14,606 test.log\n\n"
exit
fi
substrstart=$1
substrend=$2
logpath=$3
sed -n "/${substrstart}/,/${substrend}/p" ${logpath} | tee log_excerpt-$(date +%F_%H-%M-%S) | cat
Bash - Getting network data for the port (localhost only)
Getting network data:
#!/bin/bash
printf "\nCurrently for localhost only\n\n"
if [ -z $1 ]; then
printf "Please enter port and [host]\n\n"
exit
fi
port=$1
host=localhost
printf '========= lsof -i ============\n\n'
lsof -i :${port}
printf '========= netstat -lnt | grep $port ============\n\n'
netstat -lnt | grep ${port}
printf '========= nc -z $host $port ============\n\n'
nc -z ${host} ${port}
#!/bin/bash
printf "\nCurrently for localhost only\n\n"
if [ -z $1 ]; then
printf "Please enter port and [host]\n\n"
exit
fi
port=$1
host=localhost
printf '========= lsof -i ============\n\n'
lsof -i :${port}
printf '========= netstat -lnt | grep $port ============\n\n'
netstat -lnt | grep ${port}
printf '========= nc -z $host $port ============\n\n'
nc -z ${host} ${port}
Bash - kill everything with a matching name
Just a small script for killing matching processes by name:
#!/bin/bash
if [ -z $1 ]
then
printf "keyword is empty\n\n"
exit
fi
let k=0
let min=1
# mind the double brakets in do part
for i in $(ps -e | grep "$1"); do ((k++)); done
if [ "${k}" -lt "${min}" ]
then
printf "no such process to kill\n\n"
exit
fi
for i in $(ps -e | grep $1 | cut -s -f1 -d\t); do kill -9 ${i}; done
#!/bin/bash
if [ -z $1 ]
then
printf "keyword is empty\n\n"
exit
fi
let k=0
let min=1
# mind the double brakets in do part
for i in $(ps -e | grep "$1"); do ((k++)); done
if [ "${k}" -lt "${min}" ]
then
printf "no such process to kill\n\n"
exit
fi
for i in $(ps -e | grep $1 | cut -s -f1 -d\t); do kill -9 ${i}; done
Subscribe to:
Posts (Atom)