Reputation: 47
I currently have a text file called equipment.txt
and there are some records in the text file. The format of the records are StockCode|Manufacturer|Model|Description|SerialNo|Status
and the records are as follows:
S10000|HP|WM112|Wireless Mouse|11-2736-27371|Available
S10001|LOGITECH|GK2712|Gaming Keyboard|55-1662-28263|Available
S10002|DELL|GK1821|Wired Mouse|15-2541-66492|Available
S10003|LENOVO|SY1020|Keyboard|77-2661-46629|Available
S10004|PHILIPS|GN1822|Wireless Mouse|36-1638-49002|Available
S10005|HP|GV2612|Mousepad|17-2839-16392|Available
The system will allow the user to loan for the equipment. How to update the status
of the equipment (ie. from Available
to On Loan
) in equipment.txt
file after the user chooses to loan for an equipment (such as S10001
), like what codes should I write?
Edited:
I have tried this code
awk -F'|' -v id="${code^^}" 'BEGIN{ OFS="|" } $1 == code { found=1; $6="On Loan"}1' equipment.txt >> equipment.txt
, the status
of S10001
did change to On Loan
but it append the same records again at the bottom of the text file, as shown below. How to make it not appending the same records but only change the status
of that equipment only?
S10000|HP|WM112|Wireless Mouse|11-2736-27371|Available
S10001|LOGITECH|GK2712|Gaming Keyboard|55-1662-28263|Available
S10002|DELL|GK1821|Wired Mouse|15-2541-66492|Available
S10003|LENOVO|SY1020|Keyboard|77-2661-46629|Available
S10004|PHILIPS|GN1822|Wireless Mouse|36-1638-49002|Available
S10005|HP|GV2612|Mousepad|17-2839-16392|Available
S10000|HP|WM112|Wireless Mouse|11-2736-27371|Available
S10001|LOGITECH|GK2712|Gaming Keyboard|55-1662-28263|**On Loan**
S10002|DELL|GK1821|Wired Mouse|15-2541-66492|Available
S10003|LENOVO|SY1020|Keyboard|77-2661-46629|Available
S10004|PHILIPS|GN1822|Wireless Mouse|36-1638-49002|Available
S10005|HP|GV2612|Mousepad|17-2839-16392|Available
Upvotes: 0
Views: 803
Reputation: 106
sed is your friend
sed -e '/^S10001/s/Available/On Loan/' equipment.txt
Will change the status for stock code S10001. Output is like this:
$ sed -e '/^S10001/s/Available/On Loan/' equipment.txt
StockCode|Manufacturer|Model|Description|SerialNo|Status
S10000|HP|WM112|Wireless Mouse|11-2736-27371|Available
S10001|LOGITECH|GK2712|Gaming Keyboard|55-1662-28263|On Loan
S10002|DELL|GK1821|Wired Mouse|15-2541-66492|Available
S10003|LENOVO|SY1020|Keyboard|77-2661-46629|Available
S10004|PHILIPS|GN1822|Wireless Mouse|36-1638-49002|Available
S10005|HP|GV2612|Mousepad|17-2839-16392|Available
$
You could send output to a temporary file and then overwrite with it the content of original equipment.txt file.
Regards
Upvotes: 1