Bash String – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Fri, 29 Aug 2025 03:37:45 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 Bash String – Java2Blog https://java2blog.com 32 32 Bash Replace Space with Newline https://java2blog.com/bash-replace-space-with-newline/?utm_source=rss&utm_medium=rss&utm_campaign=bash-replace-space-with-newline https://java2blog.com/bash-replace-space-with-newline/#respond Fri, 29 Aug 2025 03:37:44 +0000 https://java2blog.com/?p=25069

If you have multiple consecutive spaces in the string, use the last solution, the for loop. You can use any of the following solutions if you have a multiline string.

Using tr Command

Use the tr command to replace space with newline in bash.

text="This is a sample string."
new_text=$(echo "$text" | tr ' ' '\n')
echo "$new_text"
This
is
a
sample
string.

We initialized the text variable with a sample string. Next, we piped the output of the echo command to the tr command, which translated (replaced) space (' ') with newline (\n).

We captured the output of the echo "$text" | tr ' ' '\n' command using substitution syntax ($(...)) and assigned to the new_text variable. Finally, we used the echo command to print the new_text variable’s value on the bash console.

Using awk Command

Use the awk command to replace space with newline in bash.

text="This is a sample string."
new_text=$(echo "$text" | awk -v RS=" " '{print}')
echo "$new_text"
This
is
a
sample
string.

This code snippet is similar to the previous example; however, we piped the echo command output to the awk command this time. The awk is used for text processing. It operates per line by default, but we can customize it to work with fields and records (lines) using special actions and patterns.

In the above example, we used the awk command to replace space with a newline. How? For that, we used the -v option to set the record separator variable (RS) to a space character (" "). By default, the awk takes the newline character as the value of the RS variable.

So, modifying the RS to a space character meant that each space-separated word would become a separate record for processing. For each record, the print command was executed to print the current record, followed by a new line. This effectively replaced spaces with newlines in the output.

Alternatively, we can use the following solution:

text="This is a sample string."
new_text=$(echo "$text" | awk '{gsub(/ /, "\n"); print}')
echo "$new_text"
This
is
a
sample
string.

Here, we used the gsub, an AWK function to globally substitute all occurrences of a space character (/ /) in the text with a newline character ("\n").

Using sed Command

Use the sed command to replace space with newline in bash.

text="This is a sample string."
new_text=$(echo "$text" | sed 's/ /\n/g')
echo "$new_text"
This
is
a
sample
string.

We used the sed command, which is used to process and transform text, typically used for performing text substitution, deletion, insertion and more.

In the above example, we used the sed command to substitute text. For that, we wrote the sed command sequence enclosed in single quotes; for instance, 's/ /\n/g'. This sed sequence instructed to perform a substitution operation on the input text.

The sequence of the sed command was comprised of various components; let’s learn them below:

  • s (stands for substitute) – It told the sed to find and replace.

  • / (delimiter character) – It separated the different parts of the sed command: pattern to search for, replacement, and any flags.

  • space (between first and second /): This was the pattern to search for. In our case, it was a single-space character.

  • \n (newline) – It was the replacement part; when the pattern (space) was found in the input, it would be replaced with a newline.

  • g (stands for global) – This flag told sed to perform the replacement globally, meaning all occurrences of the pattern in a line would be replaced with the newline character, not just the first occurrence.

This effectively split the input text into separate lines wherever there was a space character.

Using Parameter Expansion

Use parameter expansion to replace space with newline in bash.

text="This is a sample string."
new_text="{text// /$'\n'}"
echo "$new_text"
This
is
a
sample
string.

This example is similar to the previous ones, but we used parameter expansion this time. The parameter expansion ({text// /$'\n'}) took the value of the text variable, found all occurrences of a space character, and replaced them with a newline character. How? Following is a brief explanation of each component:

  • ${...} was the basic syntax of parameter expansion; here, ... meant anything that you want to accomplish; for instance, text// /$'\n' in our case.

  • // was a pattern-matching operator used in parameter expansion. When used as ${parameter//pattern/string}, it matched all occurrences of the provided pattern in the parameter (in this case, the variable text) and replaced them with the given string.

  • ' ' was the pattern we searched for in the text variable. It was a single-space character.

  • $'\n' was an ANSI-C quoted string representing a newline character (\n). The $'...' syntax interpreted backslash escapes in bash, so $'\n' was replaced with an actual newline character.

This was how we replaced space with newline in the text.

Using for Loop

Use the for loop to replace space with newline in bash.

text="This is a sample string."
new_text=""
for current_word in $text; do
    new_text="$new_text$current_word\n"
done
echo -e "$new_text"
This
is
a
sample
string.

We used a for loop in the above example to iterate over the $text. In each iteration, we concatenated the $new_text, $current_word, and a newline (\n) as "$new_text$current_word\n" and assigned its value to the new_text variable.

Once the loop was over, we used the echo command with the -e option to print the value of the new_text variable on the bash console.

Note that the -e option was used to interpret backslash escapes in the given string (in this case, new_text), allowing us to include special characters with specific meanings; for example, \n means a new line.

]]>
https://java2blog.com/bash-replace-space-with-newline/feed/ 0
Check If File Contains String in Bash https://java2blog.com/check-if-file-contains-string-in-bash/?utm_source=rss&utm_medium=rss&utm_campaign=check-if-file-contains-string-in-bash https://java2blog.com/check-if-file-contains-string-in-bash/#respond Sun, 26 Nov 2023 10:54:42 +0000 https://java2blog.com/?p=24631 1. Overview

Searching for strings in text files is a common task in bash, used in scenarios like log file analysis and configuration file searches. This article explores various methods to check if file contains String, including both case-sensitive and case-insensitive approaches.

2. Introduction to Problem Statement

Let’s consider a log file named server.log:

2023-11-24 10:00:00 INFO Starting server process
2023-11-24 10:00:05 ERROR Failed to bind to port 8080
2023-11-24 10:00:10 INFO Server listening on port 9090
2023-11-24 10:01:00 WARN Database connection timeout

Our goal is to check if file server.log contains string Error in it.

The expected output is something like this:

Error found in server.log.

3. Using grep

The grep is a command-line utility optimized for searching text data for lines matching a regular expression.

if grep -q "Error" server.log; then
  echo "Error found in server.log."
else
  echo "Error not found in server.log."
fi

Explanation:

  • grep -q "Error" server.log: This command searches for string Error in server.log. Here, -q stands for "quiet". It causes grep to not output anything but to exit with status 0 if the pattern is found, and 1 otherwise.
  • The if statement then checks the exit status of grep. If grep finds the string, the first block ("Error found in server.log.") is executed; if not, the else block executes.

Case-Insensitive Search:

By default, grep search is case-sensitive. To perform a case-insensitive search, use the -i flag: grep -iq "Error" server.log.

Performance:
grep is highly optimized for searching text, making it fast and efficient for this task.

4. Using awk

The awk is a versatile programming language designed for pattern scanning and processing.

Let’s use awk with if to achieve our goal:

if awk '/Error/ {found=1; exit} END {if (!found) exit 1}' server.log; then
  echo "Error found in server.log."
else
  echo "Error not found in server.log."
fi

Explanation:

  • This awk command scans server.log for the pattern Error.
  • When Error is found, awk sets a flag found to 1 and exits immediately.
  • In the END block, awk exits with status 1 if the flag found is not set, indicating that the pattern was not found.

Case-Insensitive Search:

To make the search case-insensitive in awk, we can use the tolower function:

if awk 'tolower($0) ~ /Error/ {found=1; exit} END {if (!found) exit 1}' server.log; then
  echo "Error found in server.log."
else
  echo "Error not found in server.log."
fi

Performance:
The awk is powerful for text processing but might be slightly slower than grep for simple string searches. However, it offers more flexibility for complex data manipulations.

3. Using Bash Conditional Expressions

Bash conditional expressions are a powerful feature in shell scripting, allowing for decision-making based on the evaluation of conditions within a script.

Let’s use conditional expression to check if file contains string:

if [[ $(cat server.log) == *"Error"* ]]; then
  echo "Error found in server.log."
else
  echo "Error not found in server.log."
fi

Here, cat filename.txt outputs the content of the file server.log, and the conditional [[ ... == *"Error"* ]] checks if the content contains "Error".
Case-Insensitive Search:
Bash does not directly support case-insensitive matching in this context. However, you can convert the file content and the search string to the same case:

if [[ $(cat server.log | tr '[:upper:]' '[:lower:]') == *"error"* ]]; then
  echo "Error (case-insensitive) found in server.log."
else
  echo "Error (case-insensitive) not found in server.log."
fi

Performance:
For smaller files, this method is quick and efficient. However, for larger files, its performance can degrade due to the need to read the entire file content.

6. Using sed with grep Command

The sed (Stream Editor) is a powerful and versatile text processing tool that performs text transformations on an input stream (a file or input from a pipeline).

Here, we will use sed for preprocessing and grep for final check. Let’s see with the help of example:

if sed -n '/Error/p' server.log | grep -q .; then
  echo "Error found in server.log."
else
  echo "Error not found in server.log."
fi

Explanation:

  • The sed command searches server.log for lines containing the string Error and prints them.
  • The output of sed (the matched lines) is passed to grep.
  • grep -q . checks if there is any output from sed. If there is at least one line (meaning at least one line in server.log contained "Error"), grep exits with a status of zero.
  • If grep exits with a status of zero, indicating that sed found at least one line containing "Error", the condition in the if statement is considered true, and the commands following then are executed. If no lines are found, the condition is false, and the commands after then are not executed.

Let’s understand more about sed expression sed -n '/Error/p' server.log used in above command:

  • sed: This is a stream editor for filtering and transforming text.
  • -n: This option suppresses automatic printing of pattern space. It means sed will not print anything unless explicitly told to do so.
  • '/Error/p': This is a sed command enclosed in single quotes. It tells sed to search for lines containing the string Error and print those lines (p stands for print). The /Error/ is a pattern that sed looks for in each line of the input.
    server.log: This is the file sed reads from. sed will process each line of this file, looking for the pattern Error.

7. Using Bash Loops

This method involves iterating over each line of a file to search for a string. This approach is very slow and should be only used while searching in smaller files.

found=0
while IFS= read -r line; do
  if [[ $line == *"Error"* ]]; then
    found=1
    break
  fi
done < server.log

if [[ $found -eq 1 ]]; then
  echo "Error found in server.log."
else
  echo "Error not found in server.log."
fi

Performance:
Bash loops are straightforward but can be slower, especially for larger files.

8. Searching for Multiple Strings in File

While working on script, there are often situations where we need to search based on multiples patterns rather than single pattern.

There are multiple ways to do it. Let’s see with the help of examples:

Using grep Command:

if grep -Eq 'Error|Warn' server.log; then
  echo "Error or warn found in server.log."
else
  echo "Error or warn not found in server.log."
fi

This command uses the pipe | as a logical OR to search for "Error" or "Warn".

Using awk Command

if awk '/Error/ || /Warning/' server.log; then
  echo "Error or Warning found in server.log."
else
  echo "Error or Warning not found in server.log."
fi

This awk command checks if either Error or Warn is present in server.log.

Using Bash Loops

found=0
while IFS= read -r line; do
  if [[ $line == *"Error"* ]] || [[ $line == *"Warning"* ]]; then
    found=1
    break
  fi
done < server.log

if [[ $found -eq 1 ]]; then
  echo "Error or Warning found in server.log."
else
  echo "Error or Warning not found in server.log."
fi

This Bash loop manually iterates through each line of server.log, checking for Error or Warn.

9. Searching for String in Multiple Files

To search across multiple files, use grep with a file pattern.

if grep -q "Error" *.log; then
  echo "Error found in log files."
else
  echo "Error not found in log files."
fi

We can also provide filenames separated by space.

if grep -q "Error" server1.log server2.log; then
  echo "Error found in log files."
else
  echo "Error not found in log files."
fi

10. Performance Comparison

It’s important to test how fast each method works so we can choose the best one.

We’ll create a big input server.log with 1 million lines, and test each solution on it to search pattern "Error" in the file.
To Benchmark their performance, here is the script:

#!/bin/bash

# Sample file and string
file="server.log"
string="Error"

# Function to measure using grep
measure_grep() {
    time if grep -q "$string" "$file"; then
        echo "grep: String found."
    else
        echo "grep: String not found."
    fi
}

# Function to measure using awk
measure_awk() {
    time if awk "/$string/ {found=1; exit} END {if (!found) exit 1}" "$file"; then
        echo "awk: String found."
    else
        echo "awk: String not found."
    fi
}

# Function to measure using sed
measure_sed() {
    time if sed -n "/$string/p" "$file" | grep -q .; then
        echo "sed: String found."
    else
        echo "sed: String not found."
    fi
}

# Function to measure using Bash loop
measure_bash_loop() {
    time {
        found=0
        while IFS= read -r line; do
            if [[ $line == *"$string"* ]]; then
                found=1
                break
            fi
        done < "$file"

        if [[ $found -eq 1 ]]; then
            echo "Bash loop: String found."
        else
            echo "Bash loop: String not found."
        fi
    }
}

# Function to measure using Bash Conditional Expressions
measure_bash_conditional() {
    time {
        if [[ $(cat "$file") == *"$string"* ]]; then
            echo "Bash conditional: String found."
        else
            echo "Bash conditional: String not found."
        fi
    }
}

# Execute the functions
echo "Measuring grep..."
measure_grep

echo "Measuring awk..."
measure_awk

echo "Measuring sed..."
measure_sed

echo "Measuring Bash loop..."
measure_bash_loop

echo "Measuring Bash Conditional Expressions..."
measure_bash_conditional
Measuring grep...
grep: String found.

real    0m0.073s
user    0m0.015s
sys     0m0.000s

Measuring awk...
awk: String found.

real    0m0.209s
user    0m0.125s
sys     0m0.015s
Measuring sed...
sed: String found.

real    0m0.252s
user    0m0.187s
sys     0m0.000s

Measuring Bash loop...

Bash loop: String found.

real    1m39.973s
user    0m50.156s
sys     0m34.875s
Measuring Bash Conditional Expressions...

Bash conditional: String found.

real    0m6.878s
user    0m0.687s
sys     0m2.796s

The grep command is fastest of all as it is meant for searching text data.

11. Conclusion

In this article, we have different ways for checking if file contains a String. Let’s highlight important points:

  • For simple string searching tasks in a file, grep proves to be the most efficient tool in terms of speed and CPU usage.
  • While awk and sed offer more versatility for complex text processing, they are less efficient for straightforward string searches. For example: Once it’s confirmed that the file includes the string, substitute ‘Error’ with ‘Exception’ and proceed with similar replacements.etc.
  • Bash loops and conditional expressions are significantly slower and less efficient for this task, and their use should be limited to cases where command-line tools like grep, awk, or sed are not viable.
]]>
https://java2blog.com/check-if-file-contains-string-in-bash/feed/ 0
Check If Output Contains String in Bash https://java2blog.com/bash-check-if-output-contains-string/?utm_source=rss&utm_medium=rss&utm_campaign=bash-check-if-output-contains-string https://java2blog.com/bash-check-if-output-contains-string/#respond Sun, 20 Aug 2023 08:44:17 +0000 https://java2blog.com/?p=24628 1. Overview

In this article, we will see how to check if output contains String in Bash using grep, Conditional Expressions, awk, sed commands with various options.

2. Introduction to Problem Statement

We will use ls -l to list directories and files in long format and search for .txt string in the output.
For example:

-rw-r--r-- 1 user user      0 Jul  6 09:47 Customers.xlsx
-rw-r--r-- 1 user user      0 Jul  5 03:50 File1.txt
-rw-r--r-- 1 user user      0 Jul  5 03:50 File2.txt
-rw-r--r-- 1 user user      0 Jul  5 03:50 File3.txt
-rw-r--r-- 1 user user      0 Jul  5 09:26 TestFile.md

Our goal is to search for .txt in ls -l command’s output. Expected output is:

-rw-r--r-- 1 user user      0 Jul  5 03:50 File1.txt
-rw-r--r-- 1 user user      0 Jul  5 03:50 File2.txt
-rw-r--r-- 1 user user      0 Jul  5 03:50 File3.txt

3. Using grep Command

The grep is a text-searching utility optimized for pattern matching, making it highly efficient and fast for such tasks.

Let’s see with help of example:

if ls -l | grep -q ".txt"; then
  echo "Text files found."
else
  echo "No text files found."
fi

Explanation:

  • This method uses grep -q ".txt", where -q stands for "quiet". It causes grep to not output anything but to exit with status 0 if the pattern is found, and 1 otherwise.
  • The if statement then checks the exit status of grep. If grep finds the string, the first block (echo "Text files found.") is executed; if not, the else block executes.

3.1 Searching for Pattern Using grep

We can use grep with -E option to search for pattern in the Output.

Suppose we need to find all .txt files in a folder. In this scenario, we can utilize *.txt as the pattern to match these specific files.

if ls -l | grep -qE "*.txt"; then
  echo "Text files found."
else
  echo "No text files found."
fi

The -E option enables extended regular expressions, allowing us to use patterns like *.txt. This method is particularly effective for pattern matching within scripts.

3.2 Searching with case-insensitive string

By default, grep search is case-sensitive. To do a case-insensitive search, use -i option.

if ls -l | grep -iq ".TXT"; then
  echo "Text files found."
else
  echo "No text files found."
fi

4. Using Bash Conditional Expressions

Bash conditional expressions are a powerful feature in shell scripting, allowing for decision-making based on the evaluation of conditions within a script.

4.1 Searching for Pattern

In Bash, the `[[ ... =~ ... ]] construct is used for regular expression matching. Here, the =~ operator matches the left-hand side $output with the regular expression $pattern on the right-hand side.

Let’s see with the help of example:

if [[ $(ls -l) == *".txt"* ]]; then
  echo "Text files found."
else
  echo "No text files."
fi

Here, if the match is found, the if block will execute. The [[ $output =~ $pattern]] condition evaluates as true and the message "Text files found." is printed. Otherwise, the condition evaluates as false, and the else block will execute with the message "No text files.".

4.2 Searching with case-insensitive string

In Bash version 4 and later, we can use the shopt -s nocasematch option to enable case-insensitive matching for the [[ ]] conditional expressions.

Example with shopt:

shopt -s nocasematch
if [[ $(ls -l) == *"file.txt"* ]]; then
  echo "file.txt found (case-insensitive)."
else
  echo "file.txt not found (case-insensitive)."
fi
shopt -u nocasematch

In this example, shopt -s nocasematch enables case-insensitive pattern matching within [[ ]], and shopt -u nocasematch is used to unset this option after the check.

This method is generally fast, especially for simple patterns and smaller outputs. However, it may be less efficient for complex patterns or large outputs compared to grep.

5. Using awk

awk is a powerful scripting language for text processing and is typically used for data extraction.
Let’s use awk to search string .txt in the output.

ls -l | awk '/\.txt/ {found=1} END {if(found) print "Text files found."; else print "No text files."}'

Explanation:

  • ls -l command to list file in the directory.
  • | is used to pass output of ls -lto awk command.
  • awk scans the output of ls -l for lines containing .txt.
  • It sets a flag found when the pattern is matched.
  • The END block in awk is used to print the final output based on the found flag.

This method is highly efficient, particularly for complex data manipulation.

6. Using sed with grep Command

The sed (Stream Editor) is a powerful and versatile text processing tool that performs text transformations on an input stream (a file or input from a pipeline).

Here, we will use sed for preprocessing and grep for final check. Let’s see with the help of example:

if ls -l | sed '/\.txt/!d' | grep -q .; then
  echo "Text files found."
else
  echo "No text files."
fi

Explanation:

  • sed '/\.txt/!d' deletes all lines not containing .txt.
  • The output is then piped to grep to check if there’s any remaining text.
  • This method combines sed for preprocessing with grep for final detection.

Let’s understand more about sed '/\.txt/!d':

  • sed: This is the stream editor command in Unix/Linux, used for parsing and transforming text.

  • '/.txt/!d': This is the expression passed to sed, consisting of two parts:

    • /.txt/: This part is a pattern match. It looks for lines that contain the string .txt. The dot . is escaped as . to indicate a literal period character, since in regular expressions, a dot is a special character that matches any single character.

    • !: The exclamation mark is used here to negate the pattern match. It means "for lines that do not match the pattern".

    • d: This is the delete command in sed. It deletes the lines from the output.

While sed is efficient for text processing, this method may be less performant due to the use of multiple commands and pipes.

7. Conclusion

In Bash, checking if a command’s output contains a specific string can be accomplished through various methods, each with its advantages. grep stands out for its simplicity and efficiency in pattern searching. Bash conditional expressions offer a built-in solution without external dependencies. awk provides a robust approach for more complex text processing needs. Lastly, sed, combined with grep, offers a versatile approach, though it might be less efficient for simple tasks.

]]>
https://java2blog.com/bash-check-if-output-contains-string/feed/ 0
Convert Array to Comma Separated String in Bash https://java2blog.com/bash-convert-array-to-comma-separated-string/?utm_source=rss&utm_medium=rss&utm_campaign=bash-convert-array-to-comma-separated-string https://java2blog.com/bash-convert-array-to-comma-separated-string/#respond Fri, 28 Jul 2023 18:26:17 +0000 https://java2blog.com/?p=24342 Using printf Statement

Use the printf statement to convert the array to a comma-separated string in Bash.

array=("Jan" "Feb" "Mar" "Apr")
printf -v joined '%s,' "${array[@]}"
echo "${joined%,}"
Jan,Feb,Mar,Apr

We initialized an array containing string elements. Then, we used the printf statement to convert the specified array to a comma-separated string. How? The printf statement formatted and stored the elements of the array into the joined variable, which we specified using the -v parameter.

In the printf statement, the "%s" was the format specifier for strings, which told that every element of the array must be printed as a string value and followed by a comma. The "${array[@]}" expanded the array data into individual elements, which would be formatted via a printf statement.

So, the printf -v joined '%s,' "${array[@]}" line essentially concatenated the items of the array into a string separating every item with a comma. Finally, we used the echo command to print the value of the joined variable with the last comma (trailing comma) removed.

In the echo command, the "${joined%,}" expanded the value of the joined variable and removed the trailing comma using the ${parameter%,} syntax; here, the parameter was the joined variable. Here, the % character was used as a pattern to remove the trailing comma.

Alternatively, if you don’t want to use the -v parameter, you can store the comma-separated string into a variable as follows.

array=("Jan" "Feb" "Mar" "Apr")
array=$(printf '%s,' "${array[@]}")
echo "${array%,}"
Jan,Feb,Mar,Apr

This example is similar to the previous one, but we used syntax substitution syntax here. First, we used the substitution syntax represented by the $(...) to capture the output of the printf statement, which was further assigned to the array variable; finally, we displayed it on the Bash console using the echo command.

In both examples, the delimiter, a comma (,) in our case, is preceded by the %s.

Using awk Command

Use the awk command to convert an array to a comma-separated string in Bash.

array=("Jan" "Feb" "Mar" "Apr")
awk -v sep=',' 'BEGIN{
    ORS=OFS="";
    for(i=1;i<ARGC;i++){
        print ARGV[i],ARGC-i-1?sep:""
    }
}' "${array[@]}"
Jan,Feb,Mar,Apr

After initializing the array variable, we used the awk command to convert the given array into a comma-separated string. Let’s break down the use of the awk command to understand it.

We used the -v parameter to set the value of the sep variable; we used a comma as a delimiter and initialized the sep variable with a comma. Then, we used the awk script enclosed within the single quotes, which was run before processing the input. Following is a brief explanation of each component within the script.

  • The BEGIN is an awk pattern, specifying the actions/operations to be performed before input processing. Here, the ORS=OFS="" set the ORS (Output Record Separator) and OFS (Output Field Separator) to the empty string, ensuring that no extra characters would be added between the items while printing them on the console.
  • The for loop was used to iterate over the given arguments to the awk command. The print statement within the loop printed the current argument (ARGV[i]) followed by the specified separator (sep). However, it printed an empty string rather than the separator for the last element using the ternary operator (ARGC-i-1?sep:"").
  • We used the "${array[@]}" to expand the array into separate items, which were passed as arguments to the awk command. These were the arguments on which the awk script iterated and printed them, followed by the given separator.

This way, the array was converted to comma-separated strings; you can see the output above.

Using IFS Shell Variable

Use IFS (Internal Field Separator), a shell variable, to convert the specified array to a comma separated strings in Bash.

array=("Mon" "Tue" "Wed")
IFS=,
output="${array[*]}"
echo "$output"
Mon,Tue,Wed

First, we declared and initialized the array containing three string elements. Then, we set the value of IFS, a particular shell variable, to a comma; this value would be used to separate the strings.

After that, we used parameter expansion to expand the array into separate items and joined them with the IFS separator, which we explicitly specified. Finally, we assigned the comma-separated strings to the output variable, which was further used with the echo command to display the results on the console.

Alternatively, we can do as follows:

array=("Mon" "Tue" "Wed")
(IFS=,; echo "${array[*]}")
Mon,Tue,Wed

This example is comparable to the last one. Here, in the second line of code, we created a subshell represented by (...), which was executed to temporarily modify the value of the IFS variable and printed the comma-separated string using the echo command. Note that the semi-colon was used to separate the commands from one another.

Why did we create a subshell to mutate the IFS value and expand the array elements? Because we want to ensure that the changes would be temporary and not affect the rest of the script. Using this approach, you can convert the array into a comma-separated string by temporarily mutating the value of the IFS variable and expanding the array elements using parameter expansion ("${array[*]}").

What to do to split the array into a comma-separated string without mutating the IFS variable? In that use case, we can use the following approach, which will only work as expected if the IFS variable is not mutated and set to its default value, a space character.

array=("Mon" "Tue" "Wed")
expandedArrayStr="${array[*]}"
echo "${expandedArrayStr//${IFS:0:1}/,}"
Mon,Tue,Wed

Again, we created an array named array having three string-type elements. Then, we used parameter expansion syntax to expand the array into a string and stored it in the expandedArrayStr variable. Finally, we used the echo command to print the comma-separated string on the Bash console. Let’s understand how a string was transformed into a comma-separated string.

In the echo "${expandedArrayStr//${IFS:0:1}/,}"command:

  • The ${IFS:0:1} expression expanded to the first character of the IFS variable, which by default contained a whitespace character, including a tab, newline, and a space. This extracted a substring from the IFS beginning at index 0 with a length of 1. Since the IFS was a single character, so we retrieved the first character of IFS effectively.
  • The ${expandedArrayStr//${IFS:0:1}/,} expression was a parameter expansion with a pattern substitution (whose syntax was ${inputString//pattern/replacement}). This expression substituted all occurrences of the given pattern (${IFS:0:1}, the first character of IFS) in the expandedArrayStr with replacement (,).
    Using this, all occurrences of the IFS‘s first character in expandedArrayStr would be replaced with a comma, effectively converting the array to a comma-separated string.
  • Finally, the echo command printed the results on the Bash console.

You must convert the array to a string to use this approach.

Using for Loop

To transform an array to a comma-separated string in Bash:

  • Declare and initialize an array containing string items.
  • Use the for loop to iterate over an expanded array. The array is expanded using parameter expansion.
  • In each iteration of for, we used the += operator to join the current element followed by a comma.
  • Use the echo command with ${parameter%,} syntax to remove the trailing command and print the values on the bash console.
array=("grapes" "guava" "orange")
output=""
for element in "${array[@]}"
do
    output+="$element,"
done
echo "${output%,}"
grapes,guava,orange

Here we used a for loop to join all elements.

That’s all about how to convert Array to Comma Separated String in Bash.

]]>
https://java2blog.com/bash-convert-array-to-comma-separated-string/feed/ 0
Get Everything After Character in Bash https://java2blog.com/bash-get-everything-after-character/?utm_source=rss&utm_medium=rss&utm_campaign=bash-get-everything-after-character https://java2blog.com/bash-get-everything-after-character/#respond Tue, 18 Jul 2023 11:02:34 +0000 https://java2blog.com/?p=24348 Using Parameter Expansion

Use parameter expansion to get everything after character in Bash.

string="Hello World!"
delimiter=" "
output="${string#*$delimiter}"
echo "$output"
World!

First, we set the string variable with a string type value; then, we initialised the delimiter variable with a space character (" "). The delimiter variable contained the character, after which we wanted to extract the remaining portion of the string.

After that, we used the parameter expansion as "${string#*$delimiter}" to get everything after the delimiter. Here, the # signified the removal of the shortest match of the *$delimiter from the starting of a value stored in string.

In this case, the *$delimiter matched any characters, followed by the delimiter (a space character). So, the portion of the string matched by the *$delimiter was removed, leaving everything after the specified delimiter, which we stored in the output variable. Finally, we used echo to display output on the Bash console.

Let’s see another example where the delimiter occurs multiple times.

string="Hello World! How is it going?"
delimiter=" "
output="${string#*$delimiter}"
echo "$output"
World! How is it going?

if you want to get everything after last occurrence of the character, use output="${string##*$delimiter}".

string="Hello World! How is it going?"
delimiter=" "
output="${string##*$delimiter}"
echo "$output"
going?

Alternatively, specify the starting index of the portion you want to retrieve; see the following example.

string="Hello World! How is it going?"
echo "${string:6}"
World! How is it going?

It used a 0-based index system.

Using cut Command

Use the cut command to get everything after the specified character in Bash.

string="Hello World!"
delimiter=" "
output=$(echo "$string" | cut -d "$delimiter" -f2-)
echo "$output"
World!

After initialising the string and delimiter variables, we used the substitution to grab the command’s output enclosed within the $(...) and assigned it to the output variable. How this substitution worked, let’s understand it below:

The echo command printed the value of the string variable. Here, it did not mean to display on the console but pass the value of the string variable to the next command, the cut command.

The cut is a command-line utility to extract everything after the delimiter from the string. While using cut, the -d "$delimiter" specified the delimiter to be used; in the above example, we used a space character as a delimiter.

The -f2- option chooses the sections (fields) beginning from the second field and onwards. What was the second field here? In the cut command, the fields/sections are typically defined by the given delimiter character, and the -f is used to mention which field(s) needs to be extracted.

Here, the -f2- specified that we wanted to extract all fields starting from the second field. For example, the cut command used the delimiter (a space character) to split the string ("Hello World") into fields based on the delimiter. Hello was the first field, while World was the second.

So, the -f2- indicated to capture all the fields starting from the second field. Finally, we used the echo command to display the value of the output variable on the console.

Let’s see another example to clarify the concepts using the cut command to get everything after the character.

string="Hello World! How is it going?"
delimiter=" "
output=$(echo "$string" | cut -d "$delimiter" -f2-)
echo "$output"
World! How is it going?

See, the cut command splits the string based on the delimiter and grabs the second field and onwards.

The cut command splits the input string into fields based on the given delimiter. When the delimiter occurs multiple times in the string, the cut command treats every occurrence as a separate delimiter and extracts the corresponding fields accordingly.

Using awk Command

Use the awk command to get everything after the specified character in Bash.

string="Hello  World!"
delimiter=" "
output=$(echo "$string" | awk -F "$delimiter" '{print $2}')
echo "$output"
World!

This example resembles the first example in the previous section, but we used the awk command this time. The awk split the string based on the given delimiter and then printed the second field. Here, -F was used to specify the field separator, the delimiter variable.

The {print $2} instructed the awk command to print the second field of every input line. Note that the fields in awk are identified by their positions; for instance, $1 represents the first field, $2 indicates the second field and so on.

The above code snippet will not work if the delimiter occurs multiple times in the input string.

Let’s see another example of the string will have multiple occurrences of the delimiter.

string="Hello World! How is it going?"
delimiter=" "
output=$(echo "$string" | awk -F "$delimiter" '{for (i=2; i<=NF; i++) printf "%s%s", $i, (i
World! How is it going?

In the above example, we only modified the awk code block and used a for loop to iterate through every field, from the second field (i=2) to the last field represented by the NF. We used %s%s in the printf statement to join the fields inside the loop. Note that the $i denoted the i-th field’s value in the input record.

While the (i<NF?" ":ORS) was used to append a space character between the fields, excluding the last field where the ORS (Output Record Separator, typically a newline character) was used instead.

Using sed Command

Use the sed command to get everything after the specified character in Bash.

string="Hello World!"
delimiter=" "
output=$(echo "$string" | sed "s/.*$delimiter//")
echo "$output"
World!

This code snippet is similar to those using the cut and awk commands. Here, we used the sed command, which performed the substitution operation (s/.*$delimiter//). The .*$delimiter pattern matched any character (.*) followed by a delimiter ($delimiter). We replaced the matched pattern with an empty string (//) to remove everything, including the delimiter.

Will the above script work if the delimiter occurs multiple times in the input string? NO. See the following example to learn how we can do that.

string="Hello World! How is it going?"
delimiter=" "
output=$(echo "$string" | sed "s/[^$delimiter]*$delimiter//")
echo "$output"
World! How is it going?

You are right; we only changed the sed expression. In this expression, the [^$delimiter]* matched any sequence of the characters that are not the delimiter, while the $delimiter matched the delimiter itself. Again, we replaced the matched portion with the empty string (//) to remove everything up to and including the delimiter.

Using grep Command

Use the grep command to get everything after the character in Bash.

string="Hello World!"
delimiter=" "
output=$(echo "$string" | grep -oP "(?<=${delimiter}).*")
echo "$output"
World!

This code fence also resembled the examples using cut and awk commands, initialising the string and delimiter variables with a string value and a space character, respectively. Then, we used the substitution syntax represented by $(...) to capture the pipeline output enclosed within this syntax (see the above code).

In this substitution syntax, we used the echo command to pass the value of the string variable to the grep command. The grep (Global Regular Expression Print) command is a command-line utility for pattern matching and searching within the input streams. We used the grep command with the following options:

  • -o displayed the matching portions of a line.
  • -P enabled the Perl-compatible regex (regular expressions).
  • "(?<=${delimiter}).*" regex pattern used the positive lookbehind ((? In regular expressions, a positive look-behind is a construct which lets you match the given pattern only if another pattern precedes it. The positive look-behind's syntax is (?<=…), where …` represents the pattern that should occur before the desired match.

Similarly, you can use the grep command with the same regex to get everything after the character where the specified character occurs multiple times. See the following example.

string="Hello World! How is it going?"
delimiter=" "
output=$(echo "$string" | grep -oP "(?<=${delimiter}).*")
echo "$output"
World! How is it going?

Using expr Command

Use the expr command to get everything after the character in Bash.

string="Hello World!"
delimiter=" "
delimiter_index=$(expr index "$string" "$delimiter")
start_index=$((delimiter_index + 1))
charLength=$(expr length "$string")
output=$(expr substr "$string" $start_index $charLength)
echo "$output"
World!

Here, we used the expr command. Note that we used nested expr, which means the inner expr would be evaluated first. So, we used the expr command with index function as expr index "$string" "$delimiter" to find the index of the first occurrence of the delimiter within the string, which we captured using $(...) and stored in delimiter_index variable.

After that, we added 1 to the delimiter_index and stored it in start_index. Why did we add 1? Because we want to get everything after the delimiter, excluding the delimiter itself.

Then, we used the expr command with length function as expr length "$string" to calculate the length of the string and returned the number of characters in the given string (string), which we grabbed using $(...) and stored in the charLength variable.

Finally, we used the expr command with substr function as expr substr "$string" $(expr index "$string" "$delimiter") $(expr length "$string") to extract a substring from the original string, which was the value of string in our case. It took the following three arguments:

  1. Original string ($string).
  2. The starting position of a substring.
  3. The substring’s length.

Again, we used the $(...) to capture the output of the previous expr command and assigned it to the output variable, which was further used with the echo command to display its value on the Bash console.

Similarly, we can use the expr command with a string where the specified delimiter occurs multiple times.

string="Hello World! How is it going?"
delimiter=" "
delimiter_index=$(expr index "$string" "$delimiter")
start_index=$((delimiter_index + 1))
charLength=$(expr length "$string")
output=$( expr substr "$string" $start_index $charLength)
echo "$output"
World! How is it going?

Using the IFS Variable and read Command

Use the IFS variable (a shell variable) and read command to get everything after the character in Bash.

string="Hello World!"
delimiter=" "
IFS="$delimiter" read -ra portions <<< "$string"
output="${portions[1]}"
echo "$output"
World!

Again, we initialised the string and delimiter variables with a string-type value and a space character, respectively. Then, we set the IFS variable (a shell variable) to the value of delimiter as IFS="$delimiter", which determined the field separator for the read command.

After that, the read -ra portions read the input (string), which was passed to it using the herestring (<<<) operator, and stored the separated fields into the portions array. Here, -a was used to instruct the read to store the values into an array, while -r preserved any backslashes in the input.

Finally, we assigned the second element located at index 1 of the portions array to the output variable, which was further used with echo to display on the Bash console.

What if the input string contains multiple occurrences of the delimiter? Explore the following example to see what modifications are required.

string="Hello World! How is it going?"
delimiter=" "
IFS="$delimiter" read -ra portions <<< "$string"
output="${portions[*]:1}"
echo "$output"
World! How is it going?

Everything is similar to the last example, excluding the way of extracting values from the portions array. In the "${portions[*]:1}", the ${portions[*]} joined all the array elements into a single string.

However, the :1 specified that we wanted to grab everything after the first element of the array, which effectively removed the first part of the string. As a result, the output contained everything after the specified character, the value of the delimiter.

That’s all about how to get everything after Character in Bash.

]]>
https://java2blog.com/bash-get-everything-after-character/feed/ 0
Get Text Between Two Strings in Bash https://java2blog.com/bash-get-text-between-two-strings/?utm_source=rss&utm_medium=rss&utm_campaign=bash-get-text-between-two-strings https://java2blog.com/bash-get-text-between-two-strings/#respond Tue, 18 Jul 2023 09:45:35 +0000 https://java2blog.com/?p=24410 1. Overview

In scripting and programming, extracting specific portions of text from larger strings or files is a common task. In this article, we will see different ways to get text between two String using grep, awk, sed, and bash parameter expansion.

2. Introduction to Problem Statement

We are given a String, and we need to find text between two given Strings in Bash.
For example:

Input String: Start text[Extract this]End text
Output String: [Extract this]

Our goal is to find text between Start text and End text.

3. Using grep Command with -oP option

The grep is a useful command to search matching patterns in a file or input. It becomes powerful with -o and -P options.

#!/bin/bash
string="Start text[Extract this]End text"
extracted_text=$(echo "$string" | grep -oP '(?<=Start text).*?(?=End text)')
echo "$extracted_text"
[Extract this]

The -o option is used to tell the grep to output only matched portion and the -P option is used to enable the Perl-compatible regular expressions (PCRE) for pattern matching.

Now let’s understand the regular expression:

  • (?>=Start text): This is a positive lookbehind assertion. It matches a position in the string preceded by the literal string "Start text", but it does not include "Start text" in the match.
  • .*?: This pattern matches any character (except a newline) zero or more times lazily. The ? makes the * quantifier lazy, matching as little text as possible.
  • (?=End text): This is a positive lookahead assertion. It matches a position in the string followed by the literal string "End text", but it does not include "End text" in the match.

In other words, this regular expression searched for the text that comes after "Start text" and before "End text" in the input string. Then, the command substitution syntax $() captured the command’s output inside and assigned it to the extracted_text variable, printed on the screen using the echo command.

4. Using sed Command

The sed (Stream Editor) is a powerful and versatile text processing tool that performs text transformations on an input stream (a file or input from a pipeline).

Let’s use sed to get text between two Strings.

#!/bin/bash
string="Start text [Extract this] End text"
extracted_text=$(echo "$string" | sed -n 's/Start text\(.*\)End text/\1/p')
echo "$extracted_text"
[Extract this]

Here, the -n option is used to suppress the automatic printing because, by default, sed prints each line of the input; that is why -n controls when to print.

Now let’s breakdown 's/Start text \(.*\) End text/\1/p' to understand what it means:

  • s: It represents the substitution command.
  • "Start text": It matched the literal string "Start text".
  • \(.*\): Uses parentheses to capture the text between "Start text" and "End text". The captured text is saved in a group.
  • End text: Matches the literal string " End text".
  • /\1/: This is the replacement part of the sed command. The \1 refers to the first captured group in the pattern. It is replaced with the captured text between "Start text" and "End text".
  • p: This specifies that the result should be printed.

Simply, the sed used the substitution operation s to match the given pattern and replaced it with the captured text enclosed in parentheses using the \1 backreference. Finally, the replaced line is printed due to the p flag.

5. Using awk Command

The awk is a powerful scripting language for text processing and is typically used for data extraction. The idea is to use awk with Start text and End text as delimiters and return the second column using {print $2}.

#!/bin/bash
string="Start text[Extract this]End text"
extracted_text=$(echo "$string" | awk -F 'Start text|End text' '{print $2}')
echo "$extracted_text"
[Extract this]

Here, -F is used to specify the field separator for awk. Each input line is divided into separate fields based on the occurrences of either "Start text" or "End text" as separators.

For the input string "Start text[Extract this]End text", the fields would be:

  • Field 1: ""
  • Field 2: "[Extract this]"
  • Field 3: ""

Note that the field separator pattern does not include the actual separators as part of the fields. It is used to define the boundaries for field splitting.

After that, {print $2} is used to print the field 2, which is the required text between two strings.

6. Using Bash Parameter Expansion

Bash parameter expansion offers string manipulation capabilities directly in the shell without calling external commands, which can be efficient for simple operations.

Let’s use Bash parameter expansion to achieve our goal.

#!/bin/bash
string="Start text[Extract this]End text"
extracted_text="${string#*Start text}"
extracted_text="${extracted_text%%End text*}"
echo "$extracted_text"
[Extract this]

The ${string#*Start text } removes the leading portion of the string up to the start boundary. Then, ${extracted_text%% End text*} removes the trailing portion of the string from the end boundary onwards. After that, the echo command displays the required text between two strings.

7. Conclusion

Extracting text between two strings in Bash can be achieved through various methods, each with its own advantages. grep with -oP is powerful for regex-based matching, sed excels in stream editing, awk is great for field-based text processing, and Bash parameter expansion offers a built-in solution.

]]>
https://java2blog.com/bash-get-text-between-two-strings/feed/ 0
sed Remove Leading and Trailing Whitespace https://java2blog.com/sed-remove-leading-and-trailing-whitespace/?utm_source=rss&utm_medium=rss&utm_campaign=sed-remove-leading-and-trailing-whitespace https://java2blog.com/sed-remove-leading-and-trailing-whitespace/#respond Tue, 18 Jul 2023 06:37:49 +0000 https://java2blog.com/?p=24206 Using sed Command with Substitutions

Use the sed command with multiple substitutions to remove leading and trailing whitespaces from a single-line string in Bash.

str="   It is an   example line.  "
modified_str=$( echo "$str" | sed 's/^ *//;s/ *$//;')
echo "|$str|"
echo "|$modified_str|"
|   It is an   example line.  |
|It is an   example line.|

First, we created a variable named str and set its value to a string value which contained leading and trailing whitespaces. Then, we piped the echo $str command to sed via pipeline (|) to pass the output of echo to the sed command, a stream editor in Unix-like operating systems.

We used the sed command to remove the leading and trailing whitespace; it also removed multiple consecutive occurrences of space within the line and replaced them with a single space. Further, we used the $(...) to capture the output of echo $str | sed 's/^ *//;s/ *$//;' command, which was an updated string; we stored it in modified_str variable.

After that, we used the echo command to display the value of the str and modified_str variables on the Bash console. While displaying the original and updated string, we used the | sign to clearly show leading and trailing whitespaces (you can omit | if you want).

Now, how the sed command removed the leading and trailing whitespaces. We had three consecutive substitution commands with the sed command, s/^ *// and s/ *$//. The semicolon (;) separated one substitution command from another. In each substitution command:

  • s/ – Represented the substitute command.
  • ^ – Denoted the start of the line.
  • $ – Denoted the end of the line.
  • * – Matched zero or more occurrences of the preceding space character.
  • // – It indicated an empty string between the second and third slashes, which means the matched pattern must be replaced with nothing.

So, the first substitution command (s/^ *//) matched and removed leading whitespaces at the start of every line in the input string. The second substitution command (s/ *$//) matched and removed trailing whitespaces at the end of each line in the given input string.

Use the sed command with multiple substitutions to remove leading and trailing whitespaces from a multiline string in Bash.

str="   It 
is   an example
line.  "
modified_str=$( echo "$str" | sed 's/^ *//;s/ *$//;')
echo "|$str|"
echo "|$modified_str|"
|   It 
is   an example
line.  |
|It
is   an example
line.|

This code is the same as the previous one, but we removed leading, trailing, and consecutive multiple occurrences of whitespaces from a multiline string.

Alternatively, we can use the sed command with two substitutions as follows.

str="   It 
is an    example
line.  "
modified_str=$( echo "$str" | sed -e 's/^[ \t]*//;s/[ \t]*$//')
echo "|$str|"
echo "|$modified_str|"
|   It 
is an    example
line.  |
|It
is an    example
line.|

This code snippet resembles the previous ones, excluding the sed command and removing leading, trailing, and consecutive multiple spaces within the line. Here, we used the -e option with the sed command, which specified the 's/^[ \t]*//;s/[ \t]*$//' argument as a script to be run by the sed command. Note that the 's/^[ \t]*//;s/[ \t]*$//' defined the substitution operation in the sed command.

Let’s break down the substitution command below:

  • s/ denoted the substitute command in sed, used to look for a specified pattern and perform a substitution.
  • ^ represented the start of a line.
  • $ indicated the end of a line.
  • [ \t]* pattern matched zero or more occurrences of a space or a tab character.
  • / separated the given pattern from the replacement.
  • // represented the empty string ('') between the second and third slashes denoting that the matched pattern must be replaced with nothing.

So, when we ran the above script with the specified input string (value of str variable), the sed with the mentioned script argument would look for the leading and trailing whitespaces or tabs from each line and remove them via replacing them with the empty string.

The whitespaces, tab, and newlines are removed using the substitution command. This is why you get a single-line string after removing spaces from the multiline string.

Using sed Command with POSIX Substitutions

We can also perform substitution operations using POSIX classes. Let’s explore them below.

Use the sed command with a POSIX class ([:blank:]) to remove leading and trailing whitespaces from a single-line string in Bash.

str="   It is an   example line.  "
modified_str=$( echo "$str" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
echo "|$str|"
echo "|$modified_str|"
|   It is an   example line.  |
|It is an   example line.|

Again, we set the str variable with the string value having leading and trailing whitespaces. Then, we piped the output of the echo $str to the sed command, where a substitution operation was performed using the POSIX class ([:blank:]) to eradicate leading and trailing whitespaces or tabs.

Then, we captured the modified string using the $(...) syntax and stored it in the modified_str, which was further used with the echo command to display the updated string on the console. Finally, we displayed both strings on the Bash console, the original and modified string (without leading & trailing whitespaces).

Now, how the script argument defined the substitution operation in the sed command to remove the leading and trailing whitespaces. Let’s break down the script argument ('s/^[[:blank:]]*//;s/[[:blank:]]*$//') below:

  • s/ was the substitute command in sed used to search for a particular pattern and do a substitution.
  • ^ indicated the start of a line, while $ meant the end of the line.
  • [[:blank:]]* pattern matched zero or more occurrences of a blank character (whitespaces or tabs).
  • / separated the given pattern from the replacement.
  • // denoted the empty string indicating the matched pattern must be replaced with nothing.

In the above code example, two consecutive substitution commands were separated by the semicolon. The s/^[[:blank:]]*// matched and removed the leading whitespace (tabs and spaces) at the start of each line. While the s/[[:blank:]]*$// command matched and removed trailing whitespace (tabs and spaces) at the end of every line in the given input.

Use the sed command with a POSIX class ([:blank:]) to remove leading and trailing whitespaces from a multiline string in Bash.

str="   It 
is an   example
line.  "
modified_str=$( echo "$str" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
echo "|$str|"
echo "|$modified_str|"
|   It 
is an   example
line.  |
|It
is an   example
line.|

This example is the same as above, but we removed leading and trailing whitespaces from a multiline string.

Alternatively, we can use the [:space] POSIX class as follows.

Use the sed command with a POSIX class ([:space:]) to remove leading and trailing whitespaces from a single-line string in Bash.

str="   It is an   example line.  "
modified_str=$( echo "$str" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "|$str|"
echo "|$modified_str|"
|   It is an   example line.  |
|It is an   example line.|

Use the sed command with a POSIX class ([:space:]) to remove leading and trailing whitespaces from a multiline string in Bash.

str="   It 
is    an example
line.  "
modified_str=$( echo "$str" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "|$str|"
echo "|$modified_str|"
|   It 
is    an example
line.  |
|It
is    an example
line.|

That’s all about sed remove leading and trailing whitespace.

]]>
https://java2blog.com/sed-remove-leading-and-trailing-whitespace/feed/ 0
Bash Remove Spaces from String https://java2blog.com/bash-remove-spaces-from-string/?utm_source=rss&utm_medium=rss&utm_campaign=bash-remove-spaces-from-string https://java2blog.com/bash-remove-spaces-from-string/#respond Sat, 01 Jul 2023 10:40:13 +0000 https://java2blog.com/?p=24185 Using sed Command

Use the sed command to remove spaces from string without updating the original variable in Bash.

var=" Java 2 Blog "
echo $var | sed 's/ //g'
Java2Blog

First, we stored the string value in the var variable. Then, we piped it to the sed command, a stream editor performing operations on the text streams. In the above case, we used it to replace whitespaces with nothing (i.e., removing whitespaces) in the given input string (var).

How are they removed? For that, we used sed-expression, which was 's/ //g'. Here, s denotes that it is a substitution operation. So the first / was the delimiter, which separated the given pattern to be matched and replaced.

In the above example, we matched spaces (whitespaces). As we did not want to replace the spaces with anything, no replacement was given after the second /. It means we wanted to replace the spaces with nothing that is removing spaces.

The g flag removed all spaces, not just the first one. If you want to remove the first space, omit the g flag. Finally, we used the echo statement to display the modified string on the Bash console. Note that the value in the var variable will not be modified.

Use the sed command to remove all spaces from the given string and update the original variable in Bash.

var=" Java 2 Blog "
var=$(sed 's/ //g' <<< $var)
echo $var
Java2Blog

This example is similar to the previous one, but we used <<< (here-string) to redirect the string as standard input to the specified command. In the above example, the sed 's/ //g' <<< $var command combined the sed command with << You can also store the output, returned by $(…) syntax, in a new variable rather than updating the value of the var variable.

Use the sed command to remove all spaces from the multiline string. The output value must be a single-line string containing no space characters.

var=" Java
2
Blog "
var=$(sed ':a;N;$!ba;s/\n/ /g;s/ //g' <<< $var)
echo $var
Java2Blog

The above code is similar to the last example excluding two things. First, we initialized the var variable with a multiline string value, and second, we used ':a;N;$!ba;s/\n/ /g;s/ //g' as sed-expression.

In the sed-expression, the :a;N;$!ba; construct was used to join multiple lines into a single line. It read all the lines into a pattern space and replaced the newline character (\n) with a space. How? Let's break it down further:

  • The :a was a label declaration command that we used to define a labelled called "a".
  • The N (a sed command) was used to read the next input line and appended it to the pattern space.
  • We used the $! condition to check if the current line was not the last input line.
  • The ba (a sed command) jumped to the label "a" that we defined using :a, creating a loop effectively.

Here, the ; was the separator, separating all commands from one another. Now, we used the s/ //g to remove all spaces in the modified single-line string and updated the var variable. Finally, we displayed it on the Bash console using the echo command.

Using tr Command

Use the tr command to remove all spaces from the specified string without modifying the original variable in Bash.

var=" Java 2 Blog "
echo $var | tr -d ' '
Java2Blog

First, defined the var variable and set its value to = Java 2 Blog =. Next, we piped this variable to the tr command using a pipeline (|). The tr command is a utility used to translate or delete characters; we used it to delete all occurrences of the space characters from the input string.

We used the -d option to denote that we wanted to delete characters instead of translating them. Here, ' ' was an argument provided to the tr command, mentioning the character to be removed/deleted; in the above example, it was the space character. Finally, we used the echo command to display the modified string.

Use the tr command to remove all spaces from the given string and update the original variable in Bash.

var=" Java 2 Blog "
var=$(tr -d ' ' <<< $var)
echo $var
Java2Blog

We have already learned the tr command and the -d option in this section. While we have gone through the << You can use the shopt to allow extended globing if you don’t want to use POSIX compliant way.

shopt -s extglob; var=" Java 2 Blog "; echo "{var//+([[:space:]])/}"

Using Parameter Expansion

Use parameter expansion syntax to remove all spaces from the specified string without modifying the original variable in Bash.

str=" Java 2 Blog "
echo ${str//[[:blank:]]/}
Java2Blog

We set the str variable with a string value. Then, we used the parameter expansion syntax ({var//[[:blank:]]/}) to update the value stored in the str variable. The [[:blank:]] was the character class we used to match the whitespace characters, including tabs and spaces.

The // after the str variable name represented that we wanted to replace all occurrences of the specified pattern with the given replacement. The replacement part was empty in the above code because we had nothing after the second /.

So, it matched all occurrences of whitespace characters and removed them effectively from the given input string, str. Here, the ${...} is the syntax for parameter expansion that we use in Bash. It allows us to manipulate the specified variables and substitutes their values within the strings. You can find more about parameter expansion here.

Finally, we used the echo command to print the modified string. Note that the value of the str variable was not updated. To do that, we must assign the modified string to the str variable as follows.

Use parameter expansion syntax to remove all spaces from the given string and update the original variable in Bash.

str=" Java 2 Blog "
str=${str//[[:blank:]]/}
echo $str
Java2Blog

We learned how to remove all spaces from the given string with/without updating the original variable, but what if we have to eradicate all spaces from a multiline string? Let’s see the following code to learn how to do it.

Use parameter expansion syntax to remove all spaces from the multiline string. The output value must be a single-line string containing no space characters.

multiline_str=" Java
2
Blog "
str="${multiline_str//[[:blank:]]/}"
single_line_str=$(echo "$str" | tr -d '\n')
echo $single_line_str
Java2Blog

First, we set the multiline_str variable with a multiline string value. The ${multiline_str//[[:blank:]]/} was the parameter expansion syntax that we used to remove all spaces from the given string, which was multiline_str (we have learned this syntax with the first example in this section).

Then, we stored the modified string, returned by parameter expansion syntax, into the str variable. After that, we used echo "$str" to pass the value of the str variable to the tr command via pipeline. After that, the tr deleted all newline characters from the string.

The use of the tr command is explained in detail in the [Using tr Command](Add link here) section

We used the $(...) syntax to access the value returned by the echo "$str" | tr -d '\n' command and stored it in the single_line_str variable, which was further printed on the Bash console using echo command.

Now, how did we get a single-line string? We used parameter expansion to remove all occurrences of space characters, while the tr command was used to delete newline characters. This is how we got a single-line string without any space characters.

Using awk Command with gsub Function

Use the awk command with the gsub function to remove all spaces from the specified string without modifying the original variable in Bash.

var=" Java 2 Blog "
echo $(awk '{gsub(/ /, ""); print}' <<< "$var")
Java2Blog

Again, we set the var variable with a string value containing multiple space characters. The input string, the value of the var variable, was passed to the awk command using here-string (<<<). Then, we used the awk command to process the input; how? Let’s break it down below:

The gsub() function, gsub(/ /, "") within the awk command performed a global substitution of space characters (/ /) with an empty string (""). Then, output the modified string using the print command.

Finally, we used the $(...) substitution syntax to access the value returned by the awk command; this value was further printed on the Bash console using the echo command.

Use the awk command with the gsub function to remove all spaces from the given string and update the original variable in Bash.

var=" Java 2 Blog "
var=$(awk '{gsub(/ /, ""); print}' <<< "$var")
echo $var
Java2Blog

Use the awk command with the gsub function to remove all spaces from the multiline string. The output value must be a single-line string containing no space characters.

var=" Java
2
Blog "
var=$(awk '{ gsub(/ /, ""); printf "%s", $0 }' <<< "$var")
echo $var
Java2Blog

This example code also resembles the last two code snippets, excluding the awk script, which differs slightly. Here, we used the printf "%s", $0 to print every updated line without a trailing new line. This kept all lines concatenated and resulted in a single-line string, which we assigned to the var variable. Finally, we printed the value of var using the echo command.

Using read Command

Use the read command to remove all spaces from the specified string without modifying the original variable in Bash.

str_var=" Java 2 Blog "
read -r -a words <<< $str_var
echo $(printf '%s' "${words[@]}")
Java2Blog

First, we set the str_var with a string value having multiple whitespace characters. Then, we used the read command to read the value of the str_var variable, split them into separate words, and store them in the words array. Here, << You must explicitly set the value of IFS` if you don’t want to use a space character as a delimiter.

We also used -r and -a options with read where -r ensured that backslashes would not be treated as escape characters while -a specified the words should be stored in the words array.

Next, we used the printf command to format and print the items of the words array. Here, '%s' was specified to format every item of the array while "${words[@]}" expanded the items of the words array as individual arguments to the printf command.

Finally, we used the $(...) syntax to grab the output of the printf command and printed array on the Bash console using the echo command.

Use the read command to remove all spaces from the given string and update the original variable in Bash.

str_var=" Java 2 Blog "
read -r -a words <<< $str_var
str_var=$(printf '%s' "${words[@]}")
echo $str_var
Java2Blog

You can use the tr command or any of the above approaches to remove spaces from a multiline string.

Until now, we have explored various solutions to remove all spaces from the input string, but you may have a situation where the input string has multiple occurrences of the spaces rather than a single space. We use the xargs command-line utility to remove extra occurrences of space characters.

var="    Java    2    Blog   "
echo "|$var|"
echo "|$var|" | xargs
|    Java    2    Blog   |
| Java 2 Blog |

That’s all about Bash remove spaces from String.

]]>
https://java2blog.com/bash-remove-spaces-from-string/feed/ 0
Bash Return String from Function https://java2blog.com/bash-return-string-from-function/?utm_source=rss&utm_medium=rss&utm_campaign=bash-return-string-from-function https://java2blog.com/bash-return-string-from-function/#respond Fri, 30 Jun 2023 13:14:33 +0000 https://java2blog.com/?p=24375 Using Substitution Syntax

Use substitution syntax represented by $(...) to return string from function in Bash.

function return_string(){
    echo "Welcome to Java2Blog"
}
str=$(return_string)
echo "$str"
Welcome to Java2Blog

You can use local variable as well if you want as below:

function return_string(){
    local local_str_var="Java2Blog"
    echo "Welcome to $local_str_var"
}
str=$(return_string)
echo "$str"
Welcome to Java2Blog

In this example, the return_string() function initialized a local variable named local_str_var with the "Java2Blog" value. Then, we used an echo command to print the value of the local_str_var with a custom text.

Outside the function, we used substitution syntax ($(...)) to capture the output of the return_string() function. In the above example, the retrun_string() output was the output of the echo command from the function.

We stored the captured output in an str variable, which we used with another echo to display on the Bash console.

If you don’t need to store the string for further processing, you can omit substitution syntax; see the following example.

function return_string(){
    local local_str_var="Java2Blog"
    echo "Welcome to $local_str_var"
}
return_string
Welcome to Java2Blog

Using Global Variable

Use a global variable to return string from function in Bash.

global_str_var=""
function return_string(){
    global_str_var="Java2Blog"
}
return_string
echo "$global_str_var"
Java2Blog

We initialized the global_str_var variable with an empty string. Then, inside the return_string() function, we modified the value of the global_str_var variable from an empty string to "Java2Blog". We then accessed the global_str_var variable’s modified value after invoking the function using return_string.

Using Here-String Operator

Use the here-string operator represented by <<< to return a string value from a function, which was passed to a command within the function.

function return_string(){
    local local_str_var="Java2Blog"
    cat <<< "Welcome to $local_str_var"
}
str=$(return_string)
echo "$str"
Welcome to Java2Blog

This example resembles the one where we used substitution syntax, but this time, instead of echoing, we returned the string from the function, which was passed to the cat command using the here-string (<<<) operator.

Using Here-Document Operator

Use the here-document operator to return string from function in Bash.

function return_string(){
cat << EOF
Java2Blog
EOF
}
str=$(return_string)
echo "$str"
Java2Blog

Here, we used the here-document operator that you can learn in our previously published Echo multiple lines in Bash.

That’s all about Bash return String from function.

]]>
https://java2blog.com/bash-return-string-from-function/feed/ 0