'Display all line numbers for user names starting with "admin" from passwd file

How do I find all the occurance of a particular text in a file with a shell script. (Not using grep command)



Solution 1:[1]

awk '/^admin/ {print NR;}' /etc/passwd

Solution 2:[2]

A POSIX shell-only solution could be

n=0
while n=$((n+1)); IFS=':' read -r u _
do
  case "$u" in
    admin*) echo $n;;
  esac
done < /etc/passwd

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stark
Solution 2