Grep for start of string when searching for a specific string
I have a file like this:
1 Role A
2 Role b
What I would like to do is search for the string "Role A" and return the value 1 to the variable.
So something like the following:
if grep "$i" role_info.txt
then
<assign a variable the number associated with the string>
else
<no search string found - do something else>
fi
If the columns are limited to tabs, you can do:
role='Role A'
number=$(awk -v role="$role" -F '\t' '$2==role {print $1}' role_info.txt)
If it's just spaces, try this instead:
role='Role A'
number=$(grep "$role" role_info.txt | cut -d' ' -f1)
Anyway, you can check if a match is found with:
if [[ -n $number ]]; then
# number found
else
# not found
fi
Another variant:
while read number role; do
if [[ $role == 'Role A' ]]; then
# match found
fi
done < role_info.txt
It will be a little more reliable: the role should be the second item on the line; it cannot be in the first position.
I am using awk for this. Can I guess there is a tab between the number and the video name column? If yes,
awk -F'\t' '$2 == "Role A" {print $1}' role_info.txt
To take the pattern you are looking for from a shell variable is a little tricky because you are mixing shell variables and awk columns, but it can be done:
pat="Role A"
awk -F'\t' "\$2 == \"$pat\" {print \$1}" role_info.txt
Finally, to assign the result to another shell variable:
result=`awk -F'\t' "\\$2 == \"$pat\" {print \\$1}" role_info.txt`
(In this latter case, you need to double some backslashes inside the `..)
Here's a simple way:
role=$(grep "Role A" foo.txt | awk '{print $1}')
Then, to check if a role has been assigned, check if it is empty:
if [ ! -z "$role" ]; then
echo "Role exist"
else
echo "Role does not exist"
fi
You can do it:
read -a num <<< $(grep "Role A" role_info.txt) && echo ${num[0]}
Here num
is an array, and ${num[0]}
contains1
Or that:
read num dummy <<< $(grep "Role A" role_info.txt) && echo $num
Here num
contains1
If no matches are found, the variable num
will be empty.
grep "Role A" role_info.txt → / dev / null && & && variable = 1
You can use the var value to make your decision.
How about this simple sed oneliner?
res=$(sed -ne "s/^\([0-9]\+\)[[:space:]]\+${i}/\1/p" role_info.txt)
[[ -z $res ]] && { <do something else>; }
How it works
The regular expression looks for a string like this:
[start of line] [number] [at least 1 space] [value of i]
- The number can have multiple digits. Leading 0 numbers are also allowed.
- The parentheses around the variable are redundant in this case, but I suppose that's in case you want to switch to using something like an array in the future.
If there is a match, it is stored in a variable res
. No match results in an empty variable. The second line checks if the content is res
0-length if a compound command is executed (stuff inside curly braces). Note that one-line compound commands require you to have space after the opening brace and another before the closing brace. This is necessary in order to distinguish it from bracket expansion. Of course, you can always break it down into a multi-line command.
Note . The sed command must be in double quotes to allow variable substitution. You must be careful not to pass quotes.