Skip to content

Instantly share code, notes, and snippets.

@DYelamos
Last active September 27, 2016 14:56
Show Gist options
  • Save DYelamos/b9b87028875792e674513d36f54b26a3 to your computer and use it in GitHub Desktop.
Save DYelamos/b9b87028875792e674513d36f54b26a3 to your computer and use it in GitHub Desktop.
count repeated id numbers in a file with passwd format
#!/bin/bash
# check parameters
if [ $# -eq 1 ]; then
linecount=$(cat $1 | wc -l)
echo "the file contains" $linecount "lines"
x=1
#fist loop over every line for part A of the comparison
while [ $x -le $linecount ]; do
line=$(tail -n $x $1 | head -n 1)
name=$(echo $line | cut -d : -f 1)
uid=$(echo $line | cut -d : -f 3)
y=1
count=0
#second loop over every line for part B of the comparison
while [ $y -le $linecount ]; do
line1=$(tail -n $y $1 | head -n 1)
uid1=$(echo $line1 | cut -d : -f 3)
#compares uids, if they are equal, it ++, since the count starts at 0,
#even if it appears only once it will not oversum.
if [ $uid -eq $uid1 ]; then
count=$(($count + 1))
fi
y=$(( $y + 1 ))
done
#outputs the line into exit.txt, take into consideration that it will not
#rewrite the file, only append to it.
echo $name $count >> exit.txt
x=$(( $x + 1 ))
done
else
echo "wrong parameters"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment