Pages

Wednesday, July 12, 2023

AWK File processing

 How to Use to rearrange based on 1st field

Sample Entry

File contents

benefiicary-1 16

benefiicary-1 72

benefiicary-2 6

benefiicary-2 71

benefiicary-2 84

benefiicary-3 64

benefiicary-3 76

benefiicary-3 91

benefiicary-4 60

benefiicary-1 11

benefiicary-2 23

Required output

benefiicary-1 16,72,11

benefiicary-2 6,71,84,23

benefiicary-3 64,76,91

benefiicary-4 60

USed Script

{
  if ($1 in users) users[$1] = users[$1] "," $2
  else users[$1] = $2
}

END {PROCINFO["sorted_in"] = "@ind_str_asc"; for (user in users) { print user, users[user] } }

How it works

1. AWK is a line based processing.

Line 1 Processing

So while processing 1st line below is what happening.

if ($1 in users) - $1  is 1st field in line 1 , which is benefiicary-1. AWK will check is benefiicary-1 present in array called users. As this array is just created with empty values. Condition will fail. So else block will get execute. 

Which will add entry to AWK Associative array with index as beneficiary-1 and value as  16

## Line 2

Now AWK will check is beneficiary-1 present in beneficiaries array, the condition will be true so it will execute the block under IF statement. 

Which is 

                        users[$1] = users[$1] "," $2

beneficiaries[benefiicary1] =  beneficiary[benefiicary1] "," 7  

  Array        = 6,7

So now Array beneficiaries with index benefiicary1 will have value 6,7 and the processing continues till last line.

END BLOCK

This 

Final Array benefiicary1 benefiicary2 benefiicary3 benefiicary4

6,7,11,23   6.7,8 6,7,9 6


END block will be executed like this.

for benefiicary in Array benefiicarys

print benefiicary, benefiicarys{benefiicary]

print benefiicary1 benefiicarys[benefiicary1]

  benefiicary1 6,7,11,23

Thursday, July 15, 2021

Select command in bash

 #!/bin/bash

# Define the menu list here

select brand in Samsung Sony iphone symphony Walton

do

echo "You have chosen $brand"

done


#!/bin/bash

echo "Which Operating System do you like?"


# Operating system names are used here as a data source

select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP

do


case $os in

# Two case values are declared here for matching

"Ubuntu"|"LinuxMint")

echo "I also use $os."

;;

# Three case values are declared here for matching

"Windows8" | "Windows10" | "WindowsXP")

echo "Why don't you try Linux?"

;;

# Matching with invalid data

*)

echo "Invalid entry."

break

;;

esac

done

find difference between ; and +

Difference between {} \; and {} +


find . -exec grep chrome {} \;

or

find . -exec grep chrome {} +


find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.


find . -exec grep chrome {} +

find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.

Wednesday, July 14, 2021

Ways to check reachability of remote ports

A port is a logical entity which acts as a endpoint of communication associated with an application or process on an Linux operating system. It is useful to know which ports are open and running services on a target machine before using them.


We can easily list open ports in Linux on a local machine using the netstat or several other Linux commands such NMAP.


In this guide, we will show you how to determine if ports on a remote host are reachable/open using simple netcat (in short nc) command.


netcat (or nc in short) is a powerful and easy-to-use utility that can be employed for just about anything in Linux in relation to TCP, UDP, or UNIX-domain sockets.


# yum install nc                  [On CentOS/RHEL]

# dnf install nc                  [On Fedora 22+]

$ sudo apt-get install netcat     [On Debian/Ubuntu]

We can use it to: open TCP connections, listen on arbitrary TCP and UDP ports, send UDP packets, do port scanning under both IPv4 and IPv6 and beyond.


Using netcat, you can check if a single or multiple or a range of open ports as follows. The command below will help us see if the port 22 is open on the host 192.168.56.10:


$ nc -zv 192.168.1.15 22

In the command above, the flag:


-z – sets nc to simply scan for listening daemons, without actually sending any data to them.

-v – enables verbose mode.

The next command will check if ports 80, 22 and 21 are open on the remote host 192.168.5.10 (we can use the hostname as well):

nc -zv 192.168.56.10 80 22 21


It is also possible to specify a range of ports to be scanned:’


$ nc -zv 192.168.56.10 20-80

Tuesday, July 13, 2021

builtin(command) bash

builtin command is useful to define a shell function with the same name as a shell builtin, retaining the functionality of the builtin within the function.


 The main use of this command is to define a shell function having the same name as the shell builtin by keeping the functionality of the builtin within the function.


Syntax:


builtin [shell-builtin [arg ..]]

Example: Here, we are creating a function to replace the ‘cd’ command. When you will use cd() function it will change the directory to the desktop directly.