1. Remove starting and ending " ( double quotes ) in a word
sed -e 's/^"//' -e 's/"$//' -- And for Remove all " use sed -e 's/"//g'
2. Remove carriage return and new lines
| tr -d '\n' and \r for carriage return
3. Second word after = is : awk -F '=' '{print $2}'
4. Host name to a variable : hname=$(hostname)
5. CURRENTDATE="$(date +%Y%m%d%H%M%S)"
6. if [ $# -ne 1 ] -- Number of arguments for the script is not equal to 1
7. firstarg=$1
8. mkdir -p -- if not create a directory , if exist ignore.
9. Remove last comma : sed 's/,$//'
10. Finding a private ip of a node where we have ssh access.
ph=$(ssh $nodeip '/sbin/ifconfig' >temp.txt)
prip=$(cat temp.txt | grep -e "inet:" -e "addr:" | grep -v "inet6" | grep -v "127.0.0.1" | head -n 1 | awk '{print $2}' | cut -c6-)
11.To print particular line in the given file on console.
sed -n 9406p abcd.lib
12.Find and remove all files with .gz extension from /tmp folder which are more than 4 days old.
find /tmp/ -mtime +4 -name'*.gz' -exec rm {} \;
13. Recursively search for word with numbering on file line.
grep -irn "searchword"
14. remove last two letters iff they are ab
t_param="$(echo ${s_name/%ab/})"
or
t_param="${s_name/%ab/}"
15. run bash in debug mode
set -x
export 'PS4=+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}():'
myscript.sh 2>debug.log
16. Remove filename and last / from a directory
echo "/home/abc/trail/filename.txt" | sed 's|\\(.*\\)/.*|\\1|'
17. Remove last digit from a variable
sed -i "s/$avar_name[0-9]/$avar_name/g" "file_name"
sed -e 's/^"//' -e 's/"$//' -- And for Remove all " use sed -e 's/"//g'
2. Remove carriage return and new lines
| tr -d '\n' and \r for carriage return
3. Second word after = is : awk -F '=' '{print $2}'
4. Host name to a variable : hname=$(hostname)
5. CURRENTDATE="$(date +%Y%m%d%H%M%S)"
6. if [ $# -ne 1 ] -- Number of arguments for the script is not equal to 1
7. firstarg=$1
8. mkdir -p -- if not create a directory , if exist ignore.
9. Remove last comma : sed 's/,$//'
10. Finding a private ip of a node where we have ssh access.
ph=$(ssh $nodeip '/sbin/ifconfig' >temp.txt)
prip=$(cat temp.txt | grep -e "inet:" -e "addr:" | grep -v "inet6" | grep -v "127.0.0.1" | head -n 1 | awk '{print $2}' | cut -c6-)
11.To print particular line in the given file on console.
sed -n 9406p abcd.lib
12.Find and remove all files with .gz extension from /tmp folder which are more than 4 days old.
find /tmp/ -mtime +4 -name'*.gz' -exec rm {} \;
13. Recursively search for word with numbering on file line.
grep -irn "searchword"
14. remove last two letters iff they are ab
t_param="$(echo ${s_name/%ab/})"
or
t_param="${s_name/%ab/}"
15. run bash in debug mode
set -x
export 'PS4=+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}():'
myscript.sh 2>debug.log
16. Remove filename and last / from a directory
echo "/home/abc/trail/filename.txt" | sed 's|\\(.*\\)/.*|\\1|'
17. Remove last digit from a variable
sed -i "s/$avar_name[0-9]/$avar_name/g" "file_name"
No comments:
Post a Comment