sed
删除与输入数据匹配的行的命令有什么问题?
输入数据.txt
123,
1234,
1453,
Run Code Online (Sandbox Code Playgroud)
数据删除.txt
1234,hellofirstline
123,hellosecondline
14676,hellothirdline
1453,hellofourthline
Run Code Online (Sandbox Code Playgroud)
Datatodelete.txt 中的预期输出
14676,hellothirdline
Run Code Online (Sandbox Code Playgroud)
脚本:
echo "the script starts now"
while read EachLine
do
echo $EachLine
sed "/$EachLine/d" < /home/Datatodelete.txt >/home/dummy
done < /home/InputData.txt
Run Code Online (Sandbox Code Playgroud)
您的sed
命令不起作用,因为在循环期间,每次读取一行时,它都会从完整输入文件中删除该行(并且仅删除该行),并将其输出到/home/dummy
. 这意味着输出文件每次都会被覆盖。因此,循环的第一次迭代删除了以123开头的行,但第二次迭代使用仍包含该行的原始完整文件。
试试吧grep
:
grep -vFf /home/InputData.txt /home/Datatodelete.txt > /home/dummy
Run Code Online (Sandbox Code Playgroud)
来自man grep
:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v
is specified by POSIX.)
Run Code Online (Sandbox Code Playgroud)