如何在文件的特定行中更改字符 [英] How to change a character in a specific line of a file
本文介绍了如何在文件的特定行中更改字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个.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
请参阅: 8个强大的Awk内置变量– FS,OFS,RS,ORS,NR,NF,文件名,FNR
这篇关于如何在文件的特定行中更改字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文