Bash脚本从.txt文件中删除特定行

时间:2018-01-06 11:52:08

标签: bash

我想学习如何从txt文件中删除包含用户在变量中输入的单词的行 我尝试过grep -v但是然后清除了文件的全部内容 救命?!!!

1 个答案:

答案 0 :(得分:1)

以下是一个如何存档的示例程序:

将其保存在example.sh

#!/bin/bash
word="$1" 
grep -F -v "$word"

将其保存在test.txt

Hello world
foo bar
baz bau

调用程序并将其与标准输入文件test.txt一起提供:

chmod u+x example.sh  # Need to do this only once per script (*.sh file)
./example.sh Hello < test.txt

输出(&#34; Hello world&#34;行被删除):

foo bar
baz bau
相关问题