Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is 3. Thank you in advance for your help.

What is your system? What is your shell?

unix. I'm programming in shell unix

There's no actual software product called "UNIX" that you can go and get, then install on a computer. There hasn't been for a very long time. UNIX is a blueprint now, a standard around which operating systems are designed.

UNIX-based systems often have a variety of shells to choose from anyway, varying in features, so we also need to know which one you're using.

Try uname and uname -a to find out what you're actually running.

Try echo $SHELL to find out what your shell actually is.

uname ---> Linux
uname -

---------- Post updated at 10:59 PM ---------- Previous update was at 10:57 PM ----------

uname -a Linux papis-laptop 2.6.32-24-generic #39-Ubuntu SMP Wed Jul 28 06:07:29 UTC 2010 i686 GNU/Linux
echo $SHELL --->/bin/bash

Thank u

There we go. Thanks. :slight_smile:

I'm assuming you meant "yyyy;bbbb;cccc;ddddd;eeee" because its position wouldn't be 3 otherwise...

#!/bin/bash
STR="cccc"
LINE=1
while IFS=";" read -a ARR
do
        for ((N=0; N<"${#ARR[@]}"; N++))
        do
                [[ "$STR" == "${ARR[$N]}" ]] && echo "line $LINE: $((N+1))"
        done
        ((LINE++))
done < filename.csv
1 Like

thank u very much. it's work. you save my life :smiley: