6

I'm trying to sort my users into specific servers, so what I'm trying to do at the moment is grab all the users by gender. Essentially I would have one server with all male users and one with all female users. The data in the file per line is read as so:

jimlee,7676,jim lee,sony,male,8912,1543,33:44

The command I'm using below grabs the first field and then adds the user. However my goal is to add a specific gender only and create a user for them. So seeing how the fifth field is the gender field I know that using -f5 will give me that field and would display all the users by gender but I'm not sure how to grab the name based on a specific field, in this case the fifth field (gender).

cut -d ',' -f1 file.csv | while read line; do sudo useradd "$line"; done
Zanna
  • 72,312
Danny
  • 95

4 Answers4

7

You could try

sudo awk -F , '($5=="male") {system("useradd " $1)}' file.csv
Nick Sillito
  • 1,636
7

Carrying over from my previous answer in your other question How to grab characters before comma in a list and export to a script? , use awk with $FIELD == "word". Here male and female are set in field 5

awk -F',' '$5=="male" {command=sprintf("useradd \"%s\" ",$1); system(command) }' input.txt

You can do same for "female"

2

If you want to search for a pattern and fields may be inconsistent (ie if gender might not always be in the 5th field), you could use a sed way to grab the first field if female is in any field, for example:

sed -nr 's/^([^,]+),.*,female,.*/\1/p' file

Explanation

  • -n don't print until we ask for something
  • -r use ERE
  • s/old/new replace old with new
  • ^([^,]+), save some characters before a comma at the start of each line (the first field)
  • ,.*, any number of any characters that might occur between two commas (so it doesn't matter how many fields there are between the first field and the pattern)
  • ,female, the line will only match if this pattern occurs
  • \1 the saved pattern in () from earlier
  • p print only the lines that we changed
Zanna
  • 72,312
2

It's probably quite straight forward to simply use a Bash loop with comma as field separator and calling useradd when required:

while IFS=, read user f2 f3 f4 gender _;
do
    if [ "$gender" == "male" ]; then
         sudo useradd "$user"
    fi
done < file

No need to call external commands such as awk or sed.

fedorqui
  • 10,699