bash 使用可变行号从文件中删除一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2444199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:48:51  来源:igfitidea点击:

Remove a line from a file using a variable line number

regexbashawksed

提问by paultop6

This is probably a simple one to answer, but I'm stuck, so here goes.

这可能是一个简单的答案,但我被卡住了,所以就这样吧。

sed '3d' filename    # (or something like that)

I'm having trouble trying to use a $VARIABLEinstead of the number.

我在尝试使用 a$VARIABLE而不是数字时遇到问题。

Anyone know how to get this to work, or any alternative options?

任何人都知道如何让它工作,或任何其他选择?

回答by too much php

Did you mean this:

你是不是这个意思:

bash$ VARIABLE=3
bash$ sed "${VARIABLE}d" filename

(I'm not sure if this is correct use of the sedcommand, I just know that's how you would use a variable next to a letter in bash syntax.)

(我不确定这是否正确使用sed命令,我只知道这就是在 bash 语法中使用字母旁边的变量的方式。)

回答by ghostdog74

$ variable=3
$ awk -vvar="$variable" 'NR!=var' file

using the shell(bash)

使用外壳(bash)

variable=3
i=1
while read -r line
do
  [ "$i" -ne "$variable" ] && echo "$line"
  ((i++))
done <"file" > newfile