Reputation: 7
I have a csv file that looks like this:
ID,Name,Salary,Email
23,John,3000,[email protected]
15,Mike,3200,[email protected]
16,Tom,3500,[email protected]
How can i change the salary of an employee using the ID as an identifier?
I tried
echo "Id: "
read ID
echo "New salary: "
read s
awk -F "," '{ if ($1 = $ID) $3 = $s } END{print $0}' employees.csv
Upvotes: 0
Views: 62
Reputation: 780724
Use ==
for comparison, not =
.
Shell variables aren't expanded inside single quotes. You should use the -v
1 option to assign the shell variables to awk
variables.
read -p "Id: " id
read -p "New salaray: " s
awk -F ',' -v id="$id" -v salary="$s" '$1 == id { $3 = salary; print }' employees.csv > new_employees.csv
Upvotes: 2