如果文件是:
this is line one
this is line two
this is line three
我想做类似的事情:
awk -i inplace -v line_number=$line_number 'NR == line_number {print $1}' file
假设line_number=1
。那么期望的结果是:
this
this is line two
this is line three
但相反,该文件被第 1 行的编辑版本完全替换,导致:
this
达到预期结果的正确方法是什么?
答案1
您只需应用默认{print}
规则即可其他记录例如
awk -i inplace -v line_number=$line_number 'NR == line_number {print $1; next} {print}' file
或者可能更惯用的方式
awk -i inplace -v line_number=$line_number 'NR == line_number {print $1; next} 1' file
或者你可以这样做
awk -i inplace -v line_number=$line_number 'NR == line_number {$0 = $1} 1' file