删除 bash 中具有特定字符串的每一行
Deleting every line with a specific string in bash
编辑:
我意识到我当前的代码是垃圾,但我之前尝试过的其他方法也没有用。问题是我在 Windows 上用 Notepad++ 编辑了文件,并在 Linux 上让它们 运行。程序 dos2unix 可以解决问题。
解决方法:
我在 Windows 中使用 Notepad++ 编写导致问题的文件。 运行 通过 dos2unix 修复的文件。
我写了一个 bash 脚本,它应该删除 $2 的每一行,其中包含一个在 $1 中指定的单词,并将输出写入 $3。但不知何故,它无法正常工作。
#!/bin/bash
set -f
while IFS='' read -r i || [[ -n "$i" ]]; do
sed -i "/$i/d" ""
done < ""
编辑:
例子
文件1.test:
123
678
456
文件2.test:
dasdas123dasd
3fsef344
678 3423423
r23r23rfsad
456 dasdasd
运行 脚本:
./script.sh 1.test 2.test
输出应该是:
3fsef344
r23r23rfsad
而是:
dasdas123dasd
3fsef344
678 3423423
r23r23rfsad
您每次调用 sed
时都会重新加载 </code> 并覆盖 <code>
。您应该对文件应用所有操作而不是重新加载。
#!/bin/bash
set -f
cat "" > ""
while IFS='' read -r i || [[ -n "$i" ]]; do
sed -i "/$i/d" ""
done < ""
为什么要为此使用 shell 循环和 sed?
$ grep -vFf file1 file2
3fsef344
r23r23rfsad
如果这不能满足您的需要,那么请用一个更具代表性的例子来澄清您的问题,因为 "use a shell loop calling sed multiple times" 不是任何问题的答案。请参阅 https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice 了解我这么说的一些原因。
编辑: 我意识到我当前的代码是垃圾,但我之前尝试过的其他方法也没有用。问题是我在 Windows 上用 Notepad++ 编辑了文件,并在 Linux 上让它们 运行。程序 dos2unix 可以解决问题。
解决方法: 我在 Windows 中使用 Notepad++ 编写导致问题的文件。 运行 通过 dos2unix 修复的文件。
我写了一个 bash 脚本,它应该删除 $2 的每一行,其中包含一个在 $1 中指定的单词,并将输出写入 $3。但不知何故,它无法正常工作。
#!/bin/bash
set -f
while IFS='' read -r i || [[ -n "$i" ]]; do
sed -i "/$i/d" ""
done < ""
编辑:
例子
文件1.test:
123
678
456
文件2.test:
dasdas123dasd
3fsef344
678 3423423
r23r23rfsad
456 dasdasd
运行 脚本:
./script.sh 1.test 2.test
输出应该是:
3fsef344
r23r23rfsad
而是:
dasdas123dasd
3fsef344
678 3423423
r23r23rfsad
您每次调用 sed
时都会重新加载 </code> 并覆盖 <code>
。您应该对文件应用所有操作而不是重新加载。
#!/bin/bash
set -f
cat "" > ""
while IFS='' read -r i || [[ -n "$i" ]]; do
sed -i "/$i/d" ""
done < ""
为什么要为此使用 shell 循环和 sed?
$ grep -vFf file1 file2
3fsef344
r23r23rfsad
如果这不能满足您的需要,那么请用一个更具代表性的例子来澄清您的问题,因为 "use a shell loop calling sed multiple times" 不是任何问题的答案。请参阅 https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice 了解我这么说的一些原因。