Removing Lines from CSV using Bash script

17 views
Skip to first unread message

Hamish Deas

unread,
Jul 25, 2019, 11:52:18 AM7/25/19
to GAM for G Suite
Hi Guys,

I'm attempting to create a bash script that will export a list of user email addresses as a CSV onto one of our Shared Drives, and then edit that list to remove certain users, but none of the changes I am making to the file are actually being saved to the file.

What is the best way to do this? I have attempted:

now=$(date +"%Y_%m_%d")

echo "****Creating CSV File With User List****"
if [ -d "/Volumes/GoogleDrive/Shared Drives/fileName" ]; then
  touch "/Volumes/GoogleDrive/Shared Drives/fileName/$now.csv"
  ~/bin/gam/gam print users > "/Volumes/GoogleDrive/Shared Drives/fileName/$now.csv"
  echo "Success!"
else
  echo "Computer says no!"
fi

echo "****Removing user1 and user2 from the CSV****"
if egrep -q -v "us...@domain.com$" "/Volumes/GoogleDrive/Shared Drives/fileName/$now.csv"; then
  echo "user1 has been removed from the CSV!"
else
  echo "user1 has not been removed from the CSV!"
fi
if egrep -q -v "us...@domain.com$" "/Volumes/GoogleDrive/Shared Drives/fileName/$now.csv"; then
  echo "And that is user2 removed!"
else
  echo "Something went wrong while trying to remove user2 from the CSV!"
fi

Ross Scroggs

unread,
Jul 25, 2019, 12:11:00 PM7/25/19
to google-ap...@googlegroups.com
Hamish,

egrep does not update a file in place, it writes the output to stdout.
I think you ant:
filename="/Volumes/GoogleDrive/Shared Drives/fileName/$now.csv"
if egrep -q -v "us...@domain.com$" $filename > $filename; then

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/8e4bd5d6-3f0d-4b72-980a-693cff7e8f54%40googlegroups.com.

Hamish Deas

unread,
Jul 26, 2019, 5:37:37 AM7/26/19
to GAM for G Suite
Hi Ross,

Thanks for the great advice, I didn't know that about egrep

Unfortunately egrep would just create an empty CSV file, so I ended up using sed -i which did the job really well
Reply all
Reply to author
Forward
0 new messages