ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 627
Rep:
Make the variable an array and then each group of letters becomes 0,1,2,3,4 in the array. Look up arrays in your bash tutorial. It's pretty staightforward.
Again , an array does not work for what i need .
That example is just an example , in reality that var will come with a lot of values that may or not appear depending on what user writes in terminal , and will also appear other values that i have to pick.
1st , forget array , one solution would be using tr according to SoftSprocket.
Code:
var="aaa bbb ccc ddd eee"
a1=$(echo "$var" | tr " " "\n" > tmp.file)
a2=$(grep -n "bbb" tmp.file | sed 's/[:].*$//'
)
echo "bbb is the number $a2 word"
However i will have a problem ahead , witch is :
bbb may not exist in variable but i have to look for it , and if i look for something that does not exist with grep and the -n switch then i will have an error .
Well i found a way to deal with this issue .
Was using tr as i did before but with a few changes along the code that it makes no importance to post here , since my last code solves this issue .
var="aaa bbb ccc ddd eee"
search="bbb"
a2=$(echo "$var" |
tr -s " " "\n" |
sed "/^$search\$/="
)
if [ -n "$a2" ]
then
echo "$search is the number $a2 word"
else
echo "$search not found"
fi
The a2 can be set as well with shell builtins:
Code:
a2=$(
cnt=0
set -f
for word in $var
do
((cnt+=1))
if [ "$search" = "$word" ]
then
echo "$cnt"
break
fi
done
)
As you see, the $( ) is a complete sub-shell.
It makes sense to put such lengthy code into a function, especially if you plan to use it several times.
Last edited by MadeInGermany; 02-22-2020 at 02:58 AM.
Reason: Fix: compare with $word not $var
1st , forget array , one solution would be using tr according to SoftSprocket.
Code:
var="aaa bbb ccc ddd eee"
a1=$(echo "$var" | tr " " "\n" > tmp.file)
a2=$(grep -n "bbb" tmp.file | sed 's/[:].*$//'
)
echo "bbb is the number $a2 word"
Simpler version of same thing:
Code:
var="aaa bbb ccc ddd eee"
a2=$(echo "$var" | tr " " "\n" | grep -n "bbb" | sed 's/:.*//')
echo "bbb is the number $a2 word"
but...
Quote:
However i will have a problem ahead , witch is :
bbb may not exist in variable but i have to look for it , and if i look for something that does not exist with grep and the -n switch then i will have an error .
You also have two other problems highlighted by var="aaa bbbb ccc bbb ddd eee bbb"
It's frustrating that no equivalent to ListFind(haystack,needle,delimiter) is provided natively by any CLI-based languages. Here's a quick Python implementation:
A solution for your consideration. No arrays. No loops.
Handles the "not found" case.
This program ...
Code:
#!/bin/bash Daniel B. Martin Feb20
# To execute this program, launch a terminal session and enter:
# bash /home/daniel/Desktop/LQfiles/dbm2152.bin
#
# This program inspired by ...
# https://www.linuxquestions.org/questions/programming-9/
# count-a-position-of-word-in-variable-4175670031/
# var="aaa bbb ccc ddd eeee"
# how can i know that bbb is the 2nd word in variable ?
# Keywords: word position
# File identification
Path=${0%%.*}
OutFile=$Path"out.txt"
function WordPos () {
local n=$(cut -d " " -f1 <<<$*) # n=needle
WordPos=$(tr " " "\n" <<<$* \
|nl -nln -v0 \
|grep -wm2 $n \
|tail -n1 \
|cut -d' ' -f1)
# The position of the needle in the haystack
# is in the global variable WordPos.
# WordPos=0 means the needle was not found.
} # End of function WordPos
haystack="aaa bbb ccc ddd eeee"
echo; echo "Example #1 of LQ Member danielbmartin."
needle="bbb"
echo 'Find this needle '$needle
echo ' in this haystack '$haystack
WordPos $needle $haystack
echo 'WordPos='$WordPos
echo; echo "Example #2 of LQ Member danielbmartin."
needle="cc"
echo 'Find this needle '$needle
echo ' in this haystack '$haystack
WordPos $needle $haystack
echo 'WordPos='$WordPos
echo; echo "Normal end of job."; echo; exit
... produced this result ...
Code:
Example #1 of LQ Member danielbmartin.
Find this needle bbb
in this haystack aaa bbb ccc ddd eeee
WordPos=2
Example #2 of LQ Member danielbmartin.
Find this needle cc
in this haystack aaa bbb ccc ddd eeee
WordPos=0
Normal end of job.
As always, corrections and constructive criticisms are invited.
I don't do any bash programming these days but it seems to me something like this would work:
Code:
#!/bin/bash
IFS=' ' read -ra splits <<< "$1"
searchStr="$2"
res=""
for i in "${!splits[@]}"; do
if [ "${splits[$i]}" == "${searchStr}" ]; then
res="$res$i "
fi
done
printf "${res}\n"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.