## Count the occurrences of a specific word in a .txt file in bash shell.
# tr -s ' ' '\n' < myfile.txt | grep -c 'searchword'
# cat mydr/myfiles-1-* |grep -c "searchtest"
# cat mydr/myfiles-1-* |grep -c "searchtest"
## Find Days in months(eg apr:30 days)
# tmnth=$(cal 4 2010 | egrep -v '[A-Za-z]' | wc -w)
## gunzip & untar in 1 cmd
# cat May-2010.tar.gz | gunzip -d | tar -xvf -
##
# gzcat test123.tar.gz | tar -xvf -
##
# tar -zcvf May-2010.tar.gz
##
# gzcat test123.tar.gz | tar -xvf -
##
# tar -zcvf May-2010.tar.gz
## Check is your variable is numeric or not?
# cat isnum.sh
#!/bin/bash
# Script to test variable is numeric or not
# Shirish Shukla
# Pass arg1 as number
a1=$1
a=$(echo "$a1" |awk '{if($1 > 0) print $1; else print $1"*-1"}'| bc)
b=$(echo "scale=2;$a2/$a2 + 1" | bc -l 2>/dev/null)
if [[ $b > 1 ]]
then
echo "$a1 is Numeric"
else
echo "$a1 is Non Numeric"
fi
#!/bin/bash
# Script to test variable is numeric or not
# Shirish Shukla
# Pass arg1 as number
a1=$1
a=$(echo "$a1" |awk '{if($1 > 0) print $1; else print $1"*-1"}'| bc)
b=$(echo "scale=2;$a2/$a2 + 1" | bc -l 2>/dev/null)
if [[ $b > 1 ]]
then
echo "$a1 is Numeric"
else
echo "$a1 is Non Numeric"
fi
## Output
# sh isnum.sh 12
12 is Non Numeric
# sh isnum.sh 12-5+4
12-5+4 is Non Numeric
# sh isnum.sh abc
abc is Non Numeric
# sh isnum.sh shirish-shukla
shirish-shukla is Non Numeric
# sh isnum.sh 12
12 is Non Numeric
# sh isnum.sh 12-5+4
12-5+4 is Non Numeric
# sh isnum.sh abc
abc is Non Numeric
# sh isnum.sh shirish-shukla
shirish-shukla is Non Numeric
## Multiple SED
ReplyDeletesed -e "s/['}',{']//g"
## Disable bell
set bell-style none
-- Shirish Shukla
### read multiple file at a time
ReplyDeleteexec 7<$1
exec 8<$2
#for [[ read ln1 <&7 && read ln2 <&8 ]]
while read ln1 <&7 && read ln2 <&8
#`cat $2`
#while IFS=" " read ln1 <&`cat $1`
do
echo $ln1
echo $ln2
done
-- Shirish Shukla
i want to learn linux scripting....can u suggest any of the free ebooks available in internet.....
ReplyDelete### User running process and load on your server one liner
ReplyDelete# ps -eo %u | sort |uniq -c |sort -rn;uptime |awk -F"user," '{print $2}'
--Shirish Shukla
#### replace ! symbols using sed
ReplyDelete# sed -i "s/\!1\!/\!11\!/g" source-fle
# sed -i "s/\!2\!/\!12\!/g" * ## for all files inside dir
-- Shirish Shukla
This comment has been removed by a blog administrator.
ReplyDelete#### Batch script to generate win sys info
ReplyDeleteecho List of softwares > software_list.txt
echo ================= >>software_list.txt
reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall temp1.txt
find "DisplayName" temp1.txt| find /V "ParentDisplayName" > temp2.txt
for /f "tokens=2,3 delims==" %%a in (temp2.txt) do (echo %%a >> software_list.txt)
del temp1.txt temp2.txt
## Remove spaces
ReplyDeleteecho "Shirish Shukla" | sed "s/[ \t][ \t]*//g"
--Shirish Shukla
vi tips delete first 3 chars from each line ..
ReplyDelete:%s/^.\{3}//g
--Shirish Shukla
#### WITH sed
Delete[root@SKS Shirish]# echo "123 shirish" > a
[root@SKS Shirish]# cat a
123 shirish
[root@SKS Shirish]# sed -i 's/^.\{2\}//g' a
[root@SKS Shirish]# cat a
3 shirish
[root@SKS Shirish]#
--SHIRISH SHUKLA
[root@SKS Shirish]# echo "123 shirish" > a
ReplyDelete[root@SKS Shirish]# cat a
123 shirish
[root@SKS Shirish]# sed -i 's/^.\{2\}//g' a
[root@SKS Shirish]# cat a
3 shirish
[root@SKS Shirish]#
--SHIRISH SHUKLA
### 10.3 hours before time
ReplyDeleteperl -e '$x=localtime(time-(10.5*3600));print $x'
### 5 hours after time
perl -e '$x=localtime(time+(5*3600));print $x'
--Shirish shukla
Splitting file based on column values
ReplyDelete# cat myfile.txt | awk -F"," '{gsub(/"/,""); if ($4 == "") print > "file4"; else if ($5 == "") print > "file5"; else print > "file"}'
--Shirish Shukla
Convert IST to EST ...an fantastic sorthand script script ... in bash using awk ...
ReplyDeleteecho "10:23:am" | awk -F":" '/PM|pm/ {print ($1+12)*60*60+$2*60} /AM|am/ {print $1*60*60+$2*60}' | awk '{print $1-37800}'|awk '{print strftime("%H:%M %p", $1,1) " EST"}'
EG:
DeleteTMIST="10:10:AM"
# echo $TMIST | awk -F":" '/PM|pm/ {print ($1+12)*60*60+$2*60} /AM|am/ {print $1*60*60+$2*60}' | awk '{print $1-37800}'|awk '{print strftime("%H:%M %p", $1,1) " EST"}'
23:40 PM EST
--Shirish Shukla
Encrypt - Decrypt While tar ..
ReplyDelete## Encrypt
# tar cvzf - a.sh | openssl des3 -salt -k sks | dd of=enc-info
## Try
# tar -ztvf enc-info
## Decrypt
dd if=enc-info | openssl des3 -d -k sks | tar zvfz -
-- Shirish Shukla
### Sort hand Replace a char/word from a file
ReplyDeleteeg:
# cat test
1 2 3 4 5 6 7 4 8 9 10 4
1> Using awk to wrap line replace 4
# awk '{ gsub(/4[ ]|4/,"\n"); print }' test
1 2 3
5 6 7
8 9 10
2> Using vi
%s/4/\r/g
3> Using sed
sed -i "s/4/\n/g" test
--Shirish Shukla
## If consider with and without blank space after 4 use below for that .
ReplyDelete# %s/4 \|4/\r/g
--Shirish Shukla
###Delete all white spaces lines etccc..
ReplyDeletesed -i 's/^[ \t]*//;s/[ \t]*$//'
sed -i 's/[ \t]*$//'
--Shirish Shukla
## Delete blank lines within vi?
ReplyDelete# :g/^$/d
# :g/^ *$/d
### With Awk
Delete# awk 'NF' file1 > file2
### SED
# sed -e 's/[ ^I]*$//' -e '/^$/ d'
--Shirish Shukla
## Find large files updated since 3 days
ReplyDeletefind / -xdev -type f -size +10000000c -mtime -3 -exec ls -lad {} \; |awk '{print $5/1024 "\t\t\t" $9}'
### Last 5 days updated file with least 10MB size
ReplyDelete# find /var/ -xdev -type f -size +10000000c -mtime -5 -exec ls -lad {} \; |awk '{print $5/2048 "\t\t\t" $9}'
## Find file newer than file Ita
ReplyDelete# find . -newer a | xargs du -sk {} | sort -n
## List all dir having name starting with digits ...
ReplyDelete# ll
total 0
-rw-r--r-- 1 root root 0 May 17 17:24 1
-rw-r--r-- 1 root root 0 May 17 17:24 1a
-rw-r--r-- 1 root root 0 May 17 17:24 a2
# ls -ld [[:digit:]]*
-rw-r--r-- 1 root root 0 May 17 17:24 1
-rw-r--r-- 1 root root 0 May 17 17:24 1a
--Shirish
## List files having name start with char s or p usign awk
ReplyDelete# ll -l | awk '{print $NF}' | awk '/^(s|p).*/ {print $1}'
## List files having name first char "s" and 7th char "h" i.e :" shirish*
# ll -l | awk '{print $NF}' | awk '/^(s.....h).*/ {print $1}'
--Shirish