如何在文件的特定行中更改字符
问题描述:
我有一个.bd文档,其中每行都有行.每本书都有标题,作者等,并以\t
分隔.每行的最后一个字符可以是1或0,这取决于是否借用了这本书.我需要一种更改该数字的方法,以使用id(该行的第一个数字)更改一本书的状态.
I have a .bd document which has lines for each book that I have. Each book has the title, author, etc divided by a \t
. The last character of each line can be 1 or 0 depending on whether the book is borrowed or not. I need a way to change that number change the state of a book by using the id (first number of the line).
1 title1 author1 genre1 year1 shelv1 0
2 title2 author2 genre2 year2 shelv2 0
3 title3 author3 genre3 year3 shelv3 0
答
要将ID 2的状态更改为1:
To change status of id 2 to 1:
awk -v id=2 -v status=1 'BEGIN{FS=OFS="\t"} $1==id {$NF=status}1' file
输出:
1 title1 author1 genre1 year1 shelv1 0
2 title2 author2 genre2 year2 shelv2 1
3 title3 author3 genre3 year3 shelv3 0