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.
Table of Contents
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}'
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!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
- Extracting Characters at Specific Positions:
echo "Linux" | cut -c 2-4
Output: inu - Extracting Part of a String Based on Delimiter:
echo "apple,orange,banana" | cut -d',' -f2
Output: orange
Replacing or Removing Characters in Linux
- Replacing a Specific String:
echo "Hello World" | sed 's/World/Universe/'
Output: Hello Universe - Removing Leading and Trailing Whitespace:
my_string=" Trim Spaces "
trimmed=$(echo -e "${my_string}" | tr -d '[:space:]')
Output: TrimSpaces
String Concatenation in Linux
- Joining Strings Together:
string1="Hello"
string2="World"
echo "${string1} ${string2}"
Output: Hello World
Checking String Length in Linux
- Calculating String Length:
my_string="Linux is awesome"
echo "${#my_string}"
Output: 16
Parsing String Content in Linux
- Extracting First Word from a String:
my_string="This is a test"
first_word=$(echo "${my_string}" | awk '{print $1}')
Output: This - Extracting File Extension:
filename="document.pdf"
extension="${filename##*.}"
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.