sorry my english. Im working with a script for the onboarding process for new employees, something like a "zero-touch" and im facing some issues manipulating a csv with shell bash script.
Sample of my input csv tmp.csv:
SN,HOSTNAME,OWNER,MODEL,USER
C02G1,,aaa@aaa.la,MacBook Pro,
C02G71X,HOSTNAME0001,bbbb@bbb.com,MacBook Pro,
FVFGH0S,,cccc@cccc.com,MacBook Pro,
PC24Q,,ddd@ddd.com,Macbook Air,
Desired output (could be a new file or editing the input file):
SN,HOSTNAME,OWNER,MODEL,USER
C02G1,,aaa@aaa.la,MacBook Pro,
C02G71X,HOSTNAME0001,bbbb@bbb.com,MacBook Pro,username
FVFGH0S,,cccc@cccc.com,MacBook Pro,
PC24Q,,ddd@ddd.com,Macbook Air,
My script :
#!/bin/sh
# Get the current device's serial number
SERIAL=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
# File name
tmpFile=$'tmp.csv'
# Download the CSV from Google Drive, file must be set to Shared With Anyone with Link (or Shared with Anyone)
curl -L -o $tmpFile 'https://docs.google.com/spreadsheets/d/XXXXXXXX/export?format=csv'
# Delete unwanted characters
sed -i'BAK' "s/$(printf '\r')\$//" $tmpFile
# Get username MAC
user=$(id -un)
while IFS=, read -r SN HOSTNAME OWNER MODEL USER
do
if [ "$SN" == "$SERIAL" ]
then
hostname=$HOSTNAME
awk -F, -v old="$USER" -v new="$user" '{gsub(old,new,$5);print $0}' $tmpFile > tmp3.csv
fi
done < $tmpFile
sudo scutil --set HostName $hostname
sudo scutil --set LocalHostName $hostname
sudo scutil --set ComputerName $hostname
dscacheutil -flushcache
etc etc etc......
Output with my script:
SN,HOSTNAME,OWNER,MODEL,USER
C02G1,,aaa@aaa.la,MacBook Pro,username
C02G71X,HOSTNAME0001,bbbb@bbb.com,MacBook Pro,username
FVFGH0S,,cccc@cccc.com,MacBook Pro,username
PC24Q,,ddd@ddd.com,Macbook Air,username
SUMMARY: So what i need is to add $user on the column USER of the csv only in the row that matches SN with $SERIAL
THANKS IN ADVANCE, if someone feels that this code may help i could send it when I finished it!