Bash basics for personal use
Replace using sed when too many slashes are there.
---------------------------------------------------
sed -i -e 's@/home/username/jas/@/home/username/jas/folder/@g' file.sh
sed -i -e 's@xxx.xxx.xxx.xxx@xxx.xxx.xxx.xxx@g' file.sh
Note : In bash scripting on remote server ssh login, we could experience problems with awk print $ and "`". In such situations, use cut and there will be no such issues.
To add double quotes in a line which has words separated by comma.
-----------------------------------------------------------------
sed -e 's/"//g' -e 's/[^,]*/"&"/g' -i filename
To add double quotes at end of line in a file.
-------------------------------------------------
sed -e 's/$/"/' -i filename
To add double quotes at beginning of a line in a file.
-----------------------------------------------------
sed -e 's/^/"/' -i filename
To check a log file between two dates.
--------------------------------------
awk '$0 >= "Sep 26 05:01:11" && $0 <= "Sep 26 08:37:05"' logfile
If it didn't work, use the below format.
----------------------------------------
awk '$0>=from&&$0<=to' from="2007/03/20 15:13" to="2007/08/19 14:31" logfile
If both of them didn't work use the following.
--------------------------------------------------
sed -n '/2019-10-01T04:21/,/2019-10-01T04:50/p' filename
Note : The feature with this is that it doesn't consider the position in which the date is written in the log file so you will not get error while trying to run the above command. Please do remember to adjust the date and time according to your corresponding log file.
To pass two objects using multiple for loop from single file.
-------------------------------------------------------------
for i in `cat names | awk '{print $1}'`; do for j in `cat names | grep $i | awk '{print $2}'`; do echo $i; echo $j; echo " "; done; done
for i in `cat /home/username/etc/instance.txt | awk '{print $1}'`; do for j in `cat /home/username/etc/instance.txt | grep $i | awk '{print $2}'`; do echo $i; echo $j; echo " "; done; done
Simple while loop in bash
--------------------------
while true ; do ; clear ; ps -ef|grep sed ; sleep 20 ; done
To remove unneeded stuff while filtering in log.
--------------------------------------------------------------------------------------
for i in `cat S1 | sed 's/'Local7.Info'/''/g; s/'%ASA-6-'/''/g' | tr "\t" " " | strings | grep -i --color deny | grep "xxx.xxx.xxx.xxx" | grep "xxx.xxx.xxx.xxx" | grep -i --color "xxx.xxx.xxx.xxx" | cut -d "/" -f 3 | cut -d " " -f 1 | sort -n | uniq` ; do
cat $1 | sed 's/'Local7.Info'/''/g; s/'%ASA-6-'/''/g' | tr "\t" " " | strings | grep -i --color deny | grep "xxx.xxx.xxx.xxx" | grep "xxx.xxx.xxx.xxx" | grep -i --color "xxx.xxx.xxx.xxx" | grep Si >> $1deny.txt ;
To zip file without additional disk space using gzip.
----------------------------------------------
gzip -9c console.log > console.log_$(date "+%d-%m-%y_%T").gz && > console.log &
---------------------------------------------------
sed -i -e 's@/home/username/jas/@/home/username/jas/folder/@g' file.sh
sed -i -e 's@xxx.xxx.xxx.xxx@xxx.xxx.xxx.xxx@g' file.sh
Note : In bash scripting on remote server ssh login, we could experience problems with awk print $ and "`". In such situations, use cut and there will be no such issues.
To add double quotes in a line which has words separated by comma.
-----------------------------------------------------------------
sed -e 's/"//g' -e 's/[^,]*/"&"/g' -i filename
To add double quotes at end of line in a file.
-------------------------------------------------
sed -e 's/$/"/' -i filename
To add double quotes at beginning of a line in a file.
-----------------------------------------------------
sed -e 's/^/"/' -i filename
To check a log file between two dates.
--------------------------------------
awk '$0 >= "Sep 26 05:01:11" && $0 <= "Sep 26 08:37:05"' logfile
If it didn't work, use the below format.
----------------------------------------
awk '$0>=from&&$0<=to' from="2007/03/20 15:13" to="2007/08/19 14:31" logfile
If both of them didn't work use the following.
--------------------------------------------------
sed -n '/2019-10-01T04:21/,/2019-10-01T04:50/p' filename
Note : The feature with this is that it doesn't consider the position in which the date is written in the log file so you will not get error while trying to run the above command. Please do remember to adjust the date and time according to your corresponding log file.
To pass two objects using multiple for loop from single file.
-------------------------------------------------------------
for i in `cat names | awk '{print $1}'`; do for j in `cat names | grep $i | awk '{print $2}'`; do echo $i; echo $j; echo " "; done; done
for i in `cat /home/username/etc/instance.txt | awk '{print $1}'`; do for j in `cat /home/username/etc/instance.txt | grep $i | awk '{print $2}'`; do echo $i; echo $j; echo " "; done; done
Simple while loop in bash
--------------------------
while true ; do ; clear ; ps -ef|grep sed ; sleep 20 ; done
To remove unneeded stuff while filtering in log.
--------------------------------------------------------------------------------------
for i in `cat S1 | sed 's/'Local7.Info'/''/g; s/'%ASA-6-'/''/g' | tr "\t" " " | strings | grep -i --color deny | grep "xxx.xxx.xxx.xxx" | grep "xxx.xxx.xxx.xxx" | grep -i --color "xxx.xxx.xxx.xxx" | cut -d "/" -f 3 | cut -d " " -f 1 | sort -n | uniq` ; do
cat $1 | sed 's/'Local7.Info'/''/g; s/'%ASA-6-'/''/g' | tr "\t" " " | strings | grep -i --color deny | grep "xxx.xxx.xxx.xxx" | grep "xxx.xxx.xxx.xxx" | grep -i --color "xxx.xxx.xxx.xxx" | grep Si >> $1deny.txt ;
To zip file without additional disk space using gzip.
----------------------------------------------
gzip -9c console.log > console.log_$(date "+%d-%m-%y_%T").gz && > console.log &
Comments
Post a Comment