Awk – Part 1 – Printing the 5th word in a line of text to std output

AWK

To kick off ‘Project Bash’ located here I’m going to be talking about Awk. Awk is a text processing tool that can be used to manipulate text in a line in bash. There are many uses of awk so I have called this one Part 1. In Part 1 we are going to use awk to select a word in a line of text, this can be very useful if we want a specific value in a line of text like the 5th word and want to iterate that through each line in a file. Take the following example:

So we have just used Nmap to ping sweep a subnet and want to create a target IP list from the results, we send the output to a file called alive.txt. check out the below:

Awk Part 1

If we break the command down:

The first command ‘Cat alive.txt’ prints out the contents of the file alive.txt to std output (ie into bash). We then use the command ‘grep “report”‘ to find all lines with the word ‘report’ in:

Then finally using ‘awk ‘{print $5}’ to print the 5th word in the line using the variable $5.

Changing the variable in the awk command to ‘$2’ prints the 2nd word in the line:

Ace!

Facebooktwittergoogle_pluspinterestlinkedinmail

Sed – Part 2 – To remove text from a line.

Sed – To remove text from a line.

To remove specific characters or portion of text from a line in Linux in bash we can use sed. In the below example we want to specifically remove ‘BARRY\’ from a line everything within the square brackets [text to remove] is removed, so we use:

example:

Facebooktwittergoogle_pluspinterestlinkedinmail

Sed – Part 1 – How to add a word/text to the end of each line of a file in Linux

How to add a word/text to the end of each line of a file in Linux

As the title suggests this is how we can add text to the end of each line in file in Linux.

So for example if we wanted to add ‘:80’ to the end of line of a list of IP addresses or ‘adm’ to the end of each line in a list of usernames we would use sed.

for example

sed ‘s/$/:80/’ IP.txt >  new_IP.txt

or..

 

Facebooktwittergoogle_pluspinterestlinkedinmail