# Bash Shell Linux

**URL:** <https://community.unix.com/t/bash-shell-linux/376778>\
**Category:** UNIX for Beginners Q & A\
**Created:** [November 27, 2019, 9:58pm UTC](https://community.unix.com/t/bash-shell-linux/376778 "2019-11-27T21:58:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darklord173](https://community.unix.com/letter_avatar/darklord173/32/5_5575768a8748004e209b776fc1b2916d.png) [@darklord173](https://community.unix.com/u/darklord173)\
**Post date:** [November 27, 2019, 9:58pm UTC](https://community.unix.com/t/bash-shell-linux/376778/1 "2019-11-27T21:58:44Z")

</div>

I have a csv file that looks like this

```nohighlight
Name,Food,Sport
James,Curry,Gym
Darren,Pizza,Football
Jim,Fish,Hockey
James,Sushi,Tennis

```

My code looks like this

```nohighlight
IFS=","
sed 1d $file_name | while read Name Food Sport

do

mkdir -p $Name

#echo "=================="
#echo "Name: $Name"

done <$file_name

```

I'm trying to make it so when the directory is created based on the `$name column` , it will add something to one of the duplicate names like James\_1 or JamesD rather than it only creating a directory for the first James and not the second.

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 27, 2019, 10:32pm UTC](https://community.unix.com/t/bash-shell-linux/376778/2 "2019-11-27T22:32:22Z")

</div>

Welcome to the forums.

If you are not expecting the directories to already be there (eg running the script multiple times against the same input file), you could test for the existence and increment a counter.

```nohighlight
IFS=","
file_name=infile
sed 1d $file_name | while read Name Food Sport
do
   count=0
   dirname=$Name
   while [-d "$dirname"]
   do
       ((count++))
       dirname="${Name}_$count"
   done

   mkdir -p "$dirname"
   #echo "=================="
    #echo "Name: $Name"
done

```

---

<div class="post-metadata">

**Author:** ![darklord173](https://community.unix.com/letter_avatar/darklord173/32/5_5575768a8748004e209b776fc1b2916d.png) [@darklord173](https://community.unix.com/u/darklord173)\
**Post date:** [November 27, 2019, 11:21pm UTC](https://community.unix.com/t/bash-shell-linux/376778/3 "2019-11-27T23:21:52Z")

</div>

Appreciate it bro that's perfect, made the counter= 1 from the start so now James looks like James then James 2 rather than 1 just to make it look a little better

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [November 28, 2019, 6:38am UTC](https://community.unix.com/t/bash-shell-linux/376778/4 "2019-11-28T06:38:27Z")

</div>

Two more suggestions:

- Set IFS only for the read command (unset for other commands)
- Use a builtin to omit the first line (and the while loop runs in the main shell, not forced into a sub shell by a pipe)

```nohighlight
sep=","
{
read header
while IFS=$sep read Name Food Sport
do
  ...
done
} <$file_name

```
