AWK & SED Formatting

AWK
 
[root@puppet-master script]# cat marks.txt
1) Amit English:85,Maths:90,Physics:80,Biology:87,History:30,Unix:20
2) Rahul English:05,Maths:80,Physics:30,Biology:57,History:90,Unix:90
3) Shyam English:50,Maths:99,Physics:40,Biology:67,History:70,Unix:25
4) Kedar English:83,Maths:70,Physics:50,Biology:87,History:60,Unix:28
5) Hari English:84,Maths:95,Physics:60,Biology:97,History:40,Unix:29
[root@puppet-master script]#

# awk -F"," '{ print $1" | " $2}' marks.txt (-F field-separator)
# awk -F"," '{ print $1 " "$NF}' marks.txt ($NF last field)
# awk -F":" 'NR==2,NR==5 { print $0 }' marks.txt (it will print between line 2 and 5)
# awk -F":" 'NR==5 { print length($1)}' marks.txt ( it will print the length of line)
# awk -F":" ' NR==2,NR==5 { printf "%-8s %3d\n", $1,$2 }' marks.txt ( %8s and $3d for formatting output)
# awk 'BEGIN {print "SR"," NAME" } NR==2,NR==5 {print $1" "$2 }' marks.txt (Adding Header(SR and Name))
# awk --profile 'BEGIN {printf"------|Header|-----\n" } {print} END{ printf"-----|Footer|---\n"}' marks.txt (Will add header and footer to output)
# awk -F"," '{print $1 "\t" $2 "\t" $NF}' marks.txt (It will print $3 and $4 is table format)
# awk '{print $0}' marks.txt (it will print all line)
# awk '/Amit/' marks.txt (it will search for a word Amit and print it output)
# awk '/Amit/{++cnt} END {print "Count = ",cnt }' marks.txt (it will count of Amit)
# awk 'length($0) > 71' marks.txt ( it will print line more 71 char )
# awk 'BEGIN { print ENVIRON["USER"] }' (It will print user)
# awk 'END {print FILENAME}' marks.txt (It will print file name)
# awk 'BEGIN { a = 10 ; b = 20; print "(a + b) = ", (a +b)}' (it will add a+b)
+ Addition; - Subtraction, * Multiplication, / Division
# awk 'BEGIN { a = 10; b = ++a; printf "a = %d, b = %d\n", a, b }' (Pre-Increment)
# awk 'BEGIN { a = 10; b = a++; printf "a = %d, b = %d\n", a, b }' (Post-Increment)
# awk 'BEGIN { for(i=1;i<=6;i++) print "square of", i, "is",i*i; }' (for loop )
# awk -v name=Jerry 'BEGIN {printf "NAME = %s\n", name}' (-v is variable and it will print name and variable)
# awk --dump-variables '' ; cat awkvars.out (global variables)

SED

# sed 's/Unix/Linux/1' marks.txt (it will replace 1 word Unix to Linux in all lines)
# sed 's/Unix/Linux/g' marks.txt ( It will replace all Unix with Linux)
# sed '3 s/Unix/Linux/' marks.txt (It will replace 3rd line unix word with linux)
# sed '1d' marks.txt (delete 1st line)
# sed '$d' marks.txt (delete last line)
# sed '3,$d' marks.txt (delete from 3 line to last line)
# sed -i 's/,/; /g' marks.txt (It has update the change is to marks file)

[root@puppet-master script]# cat marks.txt
1) Amit English:85, Maths:90, Physics:80, Biology:87, History:30, Unix:20
2) Rahul English:05, Maths:80, Physics:30, Biology:57, History:90, Unix:90
3) Shyam English:50, Maths:99, Physics:40, Biology:67, History:70, Unix:25
4) Kedar English:83, Maths:70, Physics:50, Biology:87, History:60, Unix:28
5) Hari English:84, Maths:95, Physics:60, Biology:97, History:40, Unix:29
[root@puppet-master script]#

SORT

# sort marks.txt (sort is ascending order)
# sort -r marks.txt (sort is descending order)
# sort marks.txt -k3 (It will sort as per column 3)
# sort marks.txt -k3 -r (It will reverse sort as per column 3)

CUT

# cut -d "," -f 1,5 marks.txt (It will print 1 and 5 column)

TEE

# sort -nr marks.txt | tee -a marks.txt (-- append)
 
If loop 
Note:
== Equal to; != Not equal to ; < Less than; <= Less then equal to ; > Greater then; >= Greater then equal to

[root@puppet-master script]#cat if-stat.sh
#!/bin/bash
read a
if (( $a == 10 )) && (( $a == 20))
then
echo "a between to 10 and 20"
elif (( $a <= 10 ))
then
echo "a is less then 10"
else
echo "a is greater then 10"
fi
[root@puppet-master script]

While Loop

[root@puppet-master script]# cat while.sh
#!/bin/bash
read a
while [ $a -lt 10 ]
do
echo "$a"
a=$(( a+1))
done
[root@puppet-master script]
 
Until Loop

#[root@puppet-master script]# cat until.sh
#!/bin/bash
read a
until [ $a -ge 10 ]
do
echo "$a"
a=$(( a+1))
done
[root@puppet-master script]
 
Case statement

 [root@puppet-master script]#cat test.sh
CheckNumber() {
   case $number in
    1 )
        echo "you had enter 1 number"
   ;;
  2 )
        echo "you had enter 2 number"
   ;;
   3 )
        echo "you had enter 3 number"
   ;;
   esac
}

# Invoke your function
echo "Enter number between 1 to 3"
read number

CheckNumber
[root@puppet-master script]#
 
For Loop

[root@puppet-master script]# cat for.sh
#!/bin/bash
#for i in 1 2 3 4 5
for i in {0..20..2}
do
echo $i
done
[root@puppet-master script]#



Practical used command for System Admin

# sed -i 's/enforcing/disabled/g' /etc/selinux/config (change enforcing to disabled)
# sed -e '/swap/ s/^#*/#/' -i /etc/fstab (add # for swap partition, if it is # do nothing)
# sed -i '/10.30.188.10/d' /etc/hosts (if 10.30.188.10 exist, remove the line)
# if ntpstat > /dev/null ; then echo 'NTP OK' ; else echo 'NTP KO' ; fi ( It will provide the NTP Status)
# dmidecode | grep -A1 "System Information" | grep Manufacturer | awk '{print $2" "$NF}'| sed 's/,//g' ( It will provide the System Information)
# df -PTh | egrep 'xfs|ext4|ext3' | sed 's/%//' | awk '{ if ( $6 > 10) print $6"% " $7 }' (using sed removed % and using if condition check partition size).
# cat /proc/loadavg | awk '{print $1}' | awk '{ if($1 > 1) printf("Current CPU Utilization is: %.2f%\n"), $0;}'
# for i in `cat servers`; do ssh $i uname -n ; done (For loop for kernel information of servers.)
# echo "test122"|mail -s "Alarm SET (TEST) for BYSG016GSR0" vallabh_darole@yahoo.co.in (Sent Mail)

To check server uptime

[root@puppet-master script]#  vi uptime.sh

file=/root/uptime/reports/Server-Uptime-Report-`date +"%d-%b-%Y"`.csv
echo "Sr,Hostname,Days,Time,User,Load Average,1 Min,5 Min,15 Min" > $file
a=1

for i in `cat /root/uptime/server-list`
do
echo -n $a ","$i","  >> $file
timeout 25 ssh $i "uptime" >> $file

if [ $? == 0 ]
then
else
echo "Login failed to "$i "."   >> $file
fi

a=$(($a + 1))
done
cat $file
[root@puppet-master script]# 

Puppet agent installation script.

[root@kvm01 ~]# cat puppet-installation.sh
#!/bin/bash
# Purpose: Puppet Agent Installation script.
# Version: 1.0
# Created Date: 8-Aug-2022
# Modified Date:
# Author : Vallabh Darole

### Install puppet repository ###

rpm -Uvh https://yum.puppet.com/puppet6-release-el-7.noarch.rpm

### Install puppet agent ###
yum install -y puppet-agent

### Update the configuration file ###
cat >  /etc/puppetlabs/puppet/puppet.conf <<EOF
[main]
certname = `hostname`
server = pup01
environment = production
runinterval = 1h
EOF

### Starting and Enabling puppet agent ###
/opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true

### Registering the puppet agent ###
/opt/puppetlabs/bin/puppet agent --test
[root@kvm01 ~]#


Installation with sshpass script.

[root@web01 ~]# cat ip.txt
web01 10.0.0.13 ansible redhat ssh ansible@10.0.0.13
[root@web01 ~]# cat script.sh
#!/bin/bash
file=ip.txt
while read -r line;
do
  ip=$(echo $line | cut -d' ' -f3)
   user=$(echo $line | cut -d' ' -f3)
   ip="$ip@"
   ip="$ip$(echo $line |cut -d ' ' -f2)"
    password="$(echo $line | cut -d ' ' -f4)"

#echo $ip $user $password


sshpass -p "$password"  ssh -n -o StrictHostKeyChecking=no $ip -C " echo $password | sudo -S yum install ntp -y;  echo $password | sudo -S systemctl status ntpd"

done < "$file"
[root@web01 ~]#


No comments:

Post a Comment