EnvSubst: Examples – Replace Environment Variables

Credentials or any other sensitive data should never be stored in configuration files (e.g. Terraform, Dockerfile, Kubernetes YAML files, etc.) and of course should never be committed to Git repositories.

Much better practice is to set some placeholders inside such files, and when needed, replace them with values from your environment variables, that can be done, for example, using a envsubst command.

In this post you will find how to install the envsubst command and examples of how to use it to replace environment variables in the files. (more…)

Vi/Vim: Dark Background – Change Color Scheme

If you use vi/vim text editor in a terminal with a dark background it may be hard to read some text due to improperly configured color scheme.

For example, dark-blue comments on a black background in vi/vim make them almost unreadable.

By default, vi/vim is configured to work with a light background, i.e. has a set background=light setting.

If you use the dark background in your terminal, you need to change the background setting in vi/vim, that will lead to adjustment of a color scheme to the dark background and make the text readable.

This short note shows how to fix colors on the dark background in vi/vim. (more…)

Vi/Vim – Backspace Not Working

If you try to delete characters in the insert mode with the backspace key in vi/vim text editor, this sometimes may not work.

Even though this is not a bug but a feature of vi/vim, you may still want to fix a backspace that is “not working”.

In this small note i will show how to fix “not working” backspace key in the insert mode in vi/vim. (more…)

RegEx: Find IP Addresses in a File Using Grep

Here are some regular expressions that will help you to perform a validation and to extract all matched IP addresses from a file.

The following regular expressions match IPv4 addresses.

Matched IP addresses can be extracted from a file using grep command.

In this article you’ll find a regular expressions themselves and an example of how to extract matched IP addresses from a file with the grep command.

Regular Expression to Match IP Addresses

Use the following regular expression to match IPv4 addresses (actually it matches all expressions from 0.0.0.0 to 999.999.999.999).

"([0-9]{1,3}[\.]){3}[0-9]{1,3}"

Grep IP Addresses

Parse a file and print all expressions that match a range between 0.0.0.0 and 999.999.999.999.

$ grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" file.txt

This regular expression is quite simple but you should understand that not all matches are technically valid IP addresses.

Let’s find only valid IP addresses with the second regular expression.

Match only Valid IPv4 Addresses

Use the following regular expression to find and validate the IPv4 addresses:

"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"

Grep Only Valid IP Addresses

Find and extract only valid IP addresses from a file:

$ grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" file.txt
Option Description
-E, –extended-regexp Use extended regular expression
-o, –only-matching Print IP addresses only

Omit -o option to print lines that contains IP addresses.

SED/AWK – Add to the End or Beginning of a Line

From time to time it is required to modify some file very fast.

And it goes without saying that the most popular command line tools for this in Linux are sed and awk – the two best text processing programs.

In the following article, you’ll find an information about how to add some text, character or comma to the beginning or to the end of every line in a file using sed and awk. (more…)

RegEx: Find Email Addresses in a File using Grep

Here is a best regular expression that will help you to perform a validation and to extract all matched email addresses from a file.

This regular expression matches 99% of the email addresses in use nowadays.

In this article you’ll find a regular expression itself and an example of how to extract matched email addresses from a file with the grep command.

Regular Expression to Match Email Addresses

Use the following regular expression to find and validate the email addresses:

"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b"

Get a List of all Email Addresses with Grep

Execute the following command to extract a list of all email addresses from a given file:

$ grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file.txt
Option Description
-E, –extended-regexp Use extended regular expression
-o, –only-matching Print email addresses only

Removing First and Last Characters From Strings in Bash

From the following article, you’ll learn how to remove first characters from the beginning of each line in Bash and how to print strings between first and last characters using cut command.

You’ll also learn how to remove last characters of each line using trick with reverse command.

I’ve created a file with the following content and I’ll use it in examples below. So lets say we need to remove first and last characters of each line from the given file.

$ cat file
12345===I Love Bash===54321
12345===I Love Bash===54321
12345===I Love Bash===54321

Remove First N Characters Of Each Line

Use the following command to delete first 5 characters of each line (trim first 5 characters and print each line starting from the 6th character):

$ cat file | cut -c 6-
===I Love Bash===54321
===I Love Bash===54321
===I Love Bash===54321

Print Strings Between First and Last Characters

Use the following command to print strings between 9th and 20th characters of each line in Bash:

$ cat file | cut -c 9-20
I Love Bash
I Love Bash
I Love Bash

Print First N Characters Of Each Line

Use the following command to print first 20 characters of each line in Bash:

$ cat file | cut -c 1-20
12345===I Love Bash
12345===I Love Bash
12345===I Love Bash

Remove Last Character Of Each Line

Using combination of reverse and cut commands we can remove last N characters of each line, as shown below. (source)

Use the following command to delete last character of each line in Bash:

$ rev file | cut -c 2- | rev
12345===I Love Bash===5432
12345===I Love Bash===5432
12345===I Love Bash===5432

Remove Last N Characters Of Each Line

Use the following command to delete last 8 characters of each line in Bash:

$ rev file | cut -c 9- | rev
12345===I Love Bash
12345===I Love Bash
12345===I Love Bash