Gabriel Boşcan
Gabriel Boşcan

Reputation: 7

How can I change only one number from a certain line in a csv format file using linux comand?

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

Answers (1)

Barmar
Barmar

Reputation: 780724

Use == for comparison, not =.

Shell variables aren't expanded inside single quotes. You should use the -v1 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

Related Questions