更改文件中的单行

时间:2011-06-26 09:57:22

标签: bash

我有一个约2000行的文件。 将特定行更改为其他行的最简单方法是什么。 比方说,我想改变第400行:int cut_off = flow_max_-6; to int cut_off = flow_max_-8;

我需要在linux控制台中执行此操作

2 个答案:

答案 0 :(得分:2)

您可以使用sed

来实现这一目标
sed -i '400s/6/8/' yourfile.c

答案 1 :(得分:1)

我会使用Vim

但您可能更喜欢:

sed -e '400,400s/6/8/' two_thousand_line_file.txt > new_two_thousand_line_file.txt

更一般地说:

sed -e '400,400s/[[:digit:]]{1,}/8/' two_thousand_line_file.txt > new_two_thousand_line_file.txt

或者:

sed -e '400,400s/\(int cut_off = flow_max_\).*\(;\)/\1some_other_number\2/' two_thousand_line_file.txt > new_two_thousand_line_file.txt