How to Find Substring Inside a String (Or How to Grep a Variable)

How to find substring inside a string (or how to grep a variable)?


LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi

How to check if a string contains a substring in Bash

You can use Marcus's answer (* wildcards) outside a case statement, too, if you use double brackets:

string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi

Note that spaces in the needle string need to be placed between double quotes, and the * wildcards should be outside. Also note that a simple comparison operator is used (i.e. ==), not the regex operator =~.

How to GREP a substring in a line which a is variable assignment?

You can use the -o ("only") flag. This command:

grep -o 'CpuIowait=[^;]*'

will print out the specific substrings that match CpuIowait=[^;]*, instead of printing out the whole lines that contain them.

Extract substring in Bash

Use cut:

echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2

More generic:

INPUT='someletters_12345_moreleters.ext'
SUBSTRING=$(echo $INPUT| cut -d'_' -f 2)
echo $SUBSTRING

How to find a substring from some text in a file and store it in a bash variable?

The following will work.

ver="$(cat config.txt | grep apache: | cut -d: -f3)"

grep apache: will find the line that has the text 'apache:' in it.

-d specifies what delimiters to use. In this case : is set as the delimiter.
-f is used to select the specific field (array index, starting at 1) of the resulting list obtained after delimiting by :

Thus, -f3 selects the 3rd occurence of the delimited list.

The version info is now captured in the variable $ver

How to select a string from a variable using grep or awk

If you're not limiting yourself to cut, this is a possible answer using sed that looks for the All files | ... | pattern and grabs the text from there.

TOTAL="$(sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/' <<< $RES)"

Here's a tiny script with that idea put to use:

#!/bin/bash

RES="----- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s ----------------------------------|---------|----------|---------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- All files | 36.95 | 20.87 | 24."
TOTAL="$(sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/' <<< $RES)"
echo "$TOTAL"

Running that:

$ ./test.sh
36.95

Grep variable at exact point in string


i only want the string that has the variable on the right-hand side, i.e. the last 8 characters

A non-regex approach using awk is better suited for this job:

s='00335439'
awk -v n=8 -v kw="$s" 'substr($0, length()-n, n) == kw' file

00043245845003354390

Here we passing n=8 to awk and using substr($0, length()-n, n) we are getting last n characters in a line, which is then compared against variable kw which is set to a value on command line.

Extract substring from a field with single awk in AIX

You can do something like this.

$ awk '{ split($2,str,"."); print str[1]"."str[2] }' file
8.0
12.01

Also, keep in mind that your cat is not needed. Simply give the file directly to awk.

grep on a variable containing a string with a tab

You probably haven't added quotes around your variable having the search pattern,

search_pattern_variable="chr11 105804693"
grep "$search_pattern_variable" input-file

Since your variable had the words chr11 105804693, shell split up the words into multiple words with the default IFS (field-separator) and had used chr11 as the pattern and 105804693 as the file-name, which it is reporting not seeing such a file.

See what Word-Splitting means in bash.



Related Topics



Leave a reply



Submit