Skip to Content

5 ways to Extract substrings in Linux

In Linux, you can manipulate strings using various commands and tools. To subtract or extract parts of a string, you might use tools like cut, awk, or string manipulation in scripting languages like Bash or Python.

Extract Characters or Substrings in Linux

Let’s say you have a string “Hello World” and want to extract the word “World”:

Using awk:

echo "Hello World" | awk '{print $2}'

Using cut:

echo "Hello World" | cut -d' ' -f2

Both commands output the second part (delimited by space) of the input string “Hello World”. 

We have many other options. These methods can be adjusted based on specific requirements and string patterns.

Using grep:

Extract parts of a string that match a pattern:

echo "apple banana cherry" | grep -o "apple\|cherry" # Output: apple cherry
echo "[email protected]" | grep -o -P "(?<=@).*(?=\.)" # Output: example

using Bash Parameter Expansion:

Extract parts of variables within scripts:

str="Hello, world!"
echo ${str:7:5} # Output: world

Using Shell Built-in Commands:

expr for simple string operations:

str="apple banana cherry"
expr substr "$str" 7 5 # Output: banan

Using Programming Languages:

Python, Perl, or other languages offer advanced string manipulation capabilities:

text = "apple banana cherry"
print(text.split()[1]) # Output: banana

More examples of Extracting Characters or Substrings in Linux

  1. Extracting Characters at Specific Positions:
    echo "Linux" | cut -c 2-4
    Output: inu
  2. Extracting Part of a String Based on Delimiter:
    echo "apple,orange,banana" | cut -d',' -f2
    Output: orange

Replacing or Removing Characters in Linux

  1. Replacing a Specific String:
    echo "Hello World" | sed 's/World/Universe/'
    Output: Hello Universe
  2. Removing Leading and Trailing Whitespace:
    my_string=" Trim Spaces "
    trimmed=$(echo -e "${my_string}" | tr -d '[:space:]')
    Output: TrimSpaces

String Concatenation in Linux

  1. Joining Strings Together:
    echo "${string1} ${string2}"
    Output: Hello World

Checking String Length in Linux

  1. Calculating String Length:
    my_string="Linux is awesome"
    echo "${#my_string}"
    Output: 16

Parsing String Content in Linux

  1. Extracting First Word from a String:
    my_string="This is a test"
    first_word=$(echo "${my_string}" | awk '{print $1}')
    Output: This
  2. Extracting File Extension:
    Output: pdf

These examples cover various operations you can perform on strings in a Linux environment, offering flexibility in managing and manipulating text-based data.