Tuesday, January 19, 2021

Linux Series - SED Utility/Package - 4. Deleting Lines

Let’s explore sed’s explicit delete command, which you specify by using the option 'd'.

Again, we are using the sample file named 'practice.txt' as an input to delete the lines. To do so, run the command "sed 'd' practice.txt".


As you see there is no outcome of the command, it is because 'd' option delete all the lines from the file if you don't mention specific pattern. 

Deleting particular line from a file

Run the command "sed '4d' practice.txt". This will print the first line from the file. Here '4' represents the fourth line of the file we would to like delete.

Incase, if you want to delete the last line of the file, you need to run the command "sed  '$d' practice.txt" . Here $ represents the last line.
Deleting range of lines  from a file

Run the command "sed '1,4p' practice.txt". This will delete the first line to fourth line from the file. Here '1 ' represents the starting number of the line and '4' represents ending number of line we would to like delete.

Incase, if you want to delete from third line to the last line of the file, you need to run the command "sed '3,$d' practice.txt"

Deleting few lines (ranges) from a file

Run the command "sed '2,+3d' test.txt". This will delete the second line to fifth line from the file. Here '2 ' represents the starting number of the line and '+3' represents three lines from second line (2+3 =5) we would to like delete.

Run the command "sed  '1~3d' practice.txt". This will delete the first line and fourth line and so on till the file exhausted. Here '1 ' represents the starting number of the line and '3' represents three lines  jump from first line we would to like delete.


Deleting lines based on the pattern from a file

I would like to delete the lines which contains the text "star".  Run the command "sed '/star/d' practice.txt"

 

Just to let you know that this will not look exactly "star" word alone and it will delete "*star*" as well. * represents any characters after 'star'. 

Deleting few lines (ranges)  based on the pattern from a file

Let us see an example with the same "practice.txt" file. This time, we can look from the line which  has "man" word to end of the file.  To do so, run the command

"sed '/man/,$d' practice.txt". Remember this command will always consider for the first match and then process till last line as its mentioned $.


Let us see another example with the same "practice.txt" file. This time, we can look from the line which  has "man" word and retrieve three lines after that.  To do so, run the command "sed  '/man/,+3d' practice.txt"

Let us see another example with the same "practice.txt" file. This time, we can look from the line which  has "man" word and retrieve until tenth line.  To do so, run the command

"sed '/man/,8d' practice.txt"


Let us see another example with the same "practice.txt" file. This time, we can look from the line which  has "man" word and retrieve until another line contains "tan"  .  To do so, run the command "sed '/man/, /tan/d' practice.txt"


Hope you now understand how sed delete the line from the file based on the given  condition 😀.

Saturday, January 9, 2021

Linux Series - SED Utility/Package - 3. Printing Lines

 In the previous example, you saw that input passed into sed with substitute operation would print the results directly to standard output. 

Let’s explore sed’s explicit print command, which you specify by using the option 'p'.

Again, we are using the same test.txt file as an input to print the lines. To do so, run the command "sed 'p' test.txt".


You’ll see line of the test.txt file printed twice.  This is because sed automatically prints each line by default, and then you’ve told it to print lines explicitly with the “p” option, so you get each line printed twice.

You can clean up the results by passing the -n option to sed, which suppresses the automatic printing.

We now are back to printing each line once.

Now, let us update few more lines in test.txt file to explain how print line works in multiple ways. The test.txt file is updated and here is the content of the file.

Printing particular line from a file

Run the command "sed -n '1p' test.txt". This will print the first line from the file. Here '1' represents the number of the line we would to like retrieve.

Incase, if you want to print the last line of the file, you need to run the command "sed -n '$p' test.txt" . Here $ represents the last line.

Printing range of lines  from a file

Run the command "sed -n '1,4p' test.txt". This will print the first line to fourth line from the file. Here '1 ' represents the starting number of the line and '4' represents ending number of line we would to like retrieve.

Incase, if you want to print  from third line to the last line of the file, you need to run the command "sed -n '3,$p' test.txt"

Printing few lines (ranges) from a file

Run the command "sed -n '2,+3p' test.txt". This will print the second line to fifth line from the file. Here '2 ' represents the starting number of the line and '+3' represents three lines from second line (2+3 =5) we would to like retrieve.

Run the command "sed -n '1~3p' test.txt". This will print the first line and fourth line and so on till the file exhausted. Here '1 ' represents the starting number of the line and '3' represents three lines  jump from first line we would to like retrieve.

For instance, '50~5p' matches line number 50, 55, 60, 65, and so on until file data exhausted. 


Printing lines based on the pattern from a file

 pattern range can be a simple text or a complex regular expression. Let us take an example. I have a file called "Rahul.txt" and I would like to print the lines which contains the text "cricket"

Run the command "sed -n '/cricket/p' Rahul.txt"

Just to let you know that this will not look exactly "cricket" word alone and it will print "cricket*" as well. * represents any characters after cricket. Let us see another example with the same "Rahul.txt" file. This time we can look for "Depend" word from the file.


As you see the result prints the line which has "Dependable".

Printing few lines (ranges)  based on the pattern from a file

Let us see an example with the same "Rahul.txt" file. This time, we can look from the line which  has "Karnataka" word to end of the file.  To do so, run the command

"sed -n '/Karnataka/,$p' Rahul.txt". Remember this command will always consider for the first match and then process till last line as its mentioned $.

Let us see another example with the same "Rahul.txt" file. This time, we can look from the line which  has "Karnataka" word and retrieve three lines after that.  To do so, run the command

"sed -n '/Karnataka/,+3p' Rahul.txt"


Let us see another example with the same "Rahul.txt" file. This time, we can look from the line which  has "Karnataka" word and retrieve until tenth line.  To do so, run the command

"sed -n '/Karnataka/,10p' Rahul.txt"


Let us see another example with the same "Rahul.txt" file. This time, we can look from the line which  has "gentleman" word and retrieve until another line contains "Karnataka"  .  To do so, run the command

"sed -n '/gentleman/, /Karnataka/p' Rahul.txt"


Hope you now understand how sed prints the line from the file based on the given  condition 😀.

Linux Series - SED Utility/Package - 2. Input Methods

As mentioned in this article, sed reads the input from two different ways.

1. via file

   Example: Let us create a file called test.txt and it contains the text called Linux.


This file is an input to sed command. Let us see how sed process this input file.
Run the command "sed 's/Linux/Windows/' test.txt". 

Basically, option "s" passes in sed command when you want to substitute the text with another text.  "s" means substitution
sed reads the input of test.txt file and it replaces the word "Linux" with "Windows" and displayed in the terminal.  However, this above command will not replace "Linux" with "Windows" in actual file (test.txt).  Let us confirm it.

2. via pipe
Hope you know how pipe works in linux. If not, the below picture represents how pipe works.
Output of command1 will be the input for command2.
Example: Let us use pipe in sed command to understand how it works. For this, we are again using the same test.txt file.

When you execute "cat test.txt" command will return the text "Linux" which is the input to sed command where sed replaces "Linux" with "Windows".

Hope you understand how sed reads the input and process the command😀.





Linux Series - SED Utility/Package - 1. Installation

When you are working in Linux, you might have heard the term called 'SED' utility. Even if you don't, it is always good to know SED utility which makes your job easier when you are manipulating text.

'sed' stands for stream editor.  

  • It is a powerful text stream editor and can perform insertion, deletion, search and replace(substitution) on the input.
  • It supports regular expression which allows to perform complex pattern matching.

SED reads a line from the input stream (file, pipe, or stdin) and display the outcome in stdout. Also, it can update the output in the file as well when the option 'i' is passed along with sed command. We can cover this later in the series)

Lets stop theoretical definitions and go on for hands on to understand more about sed utility.

First job is to check sed package installed in your linux environment. To do so,

run the command "sed --version"

As you see, sed package is not installed in my environment, you need to install it.
To do so, run the command "sudo apt-get install -y sed" (Make sure you are running the command with super user access and you need to run "sudo yum install -y sed" if you are using centos linux distribution)


Now let us run the command (sed --version) to check sed package installed in my linux environment.


This is how you need to make sure the installation of sed package in your linux environment.

Hope you understand now 😀. 




Tuesday, September 8, 2020

Jmeter Controllers - Loop Controller

This is sixth controller in Jmeter Controllers series. If you don't read the previous posts in this series, I would suggest you to read it here.

6. Loop Controller

This controller executes the samplers for specified number of times in the Thread Group. This controller will be useful when you want to hit particular request multiple times. Post payment is one of the classic example for this case. When you make the payment call, the initial response for the status is PENDING and it will take few seconds to update as ACCEPTED/SUCCESSFUL. So you have to keep on hitting this payment call again and evaluate the response each time if the status is changed to ACCEPTED/SUCCESSFUL.

Let us see how this controller works in practical.

Step # 1: Add the Loop controller into test plan. Right click on Thread Group -> Add -> Logic controllers -> Loop Controller

Step # 2: In this test plan, I have 3 HTTP samplers. Out of 3 samplers, I placed 1 sampler under Loop Controller. I am going to execute the test plan for 1 iteration with 1 thread. Loop count is specified as 5 in Loop controller

Step # 3: Add listener "View Results Tree" to the test plan.

Step # 4: Execute the test plan.

As you see the above snapshot, SINGLE USER is executed 5 times (based on loop count) though the test plan is executed for only one iteration. Also, other samplers are executed only once as they are not placed in Loop controller.

That's all. Simple, isnt' it?

Linux Series - SED Utility/Package - 4. Deleting Lines

Let’s explore sed’s explicit delete command, which you specify by using the option 'd'. Again, we are using the sample file named &#...