如何从文本文件中删除所有不包含`@`的行?

时间:2022-10-31 13:08:13

I cannot get this, or many other combinations of this, to work:

我无法使用此功能或其他许多组合来实现:

sed -i '' '/@/!d' file.txt
sed -i '' '/\@/!d' file.txt
sed -n '/@/p' file.txt > newfile.tx

What am I missing?

我错过了什么?

file.txt:

1. Lorem ip@sum dolor sit amet, consectetur adipiscing elit.
2. Nunc eu justo quis diam tempus auctor.
3. Suspe@ndisse in nulla et tellus aliquet finibus.
4. Aliquam @quis diam in tortor euismod faucibus ac lobortis massa.
5. Aenean feugiat nibh lobortis maximus pharetra.
6. Sed pharetra nibh id est lacinia, non pharetra nisi molestie.

desired result after sed or awk or grep or ??? deleting all lines not containing an @:

sed或awk或grep之后的期望结果或???删除所有不包含@的行:

1. Lorem ip@sum dolor sit amet, consectetur adipiscing elit.
3. Suspe@ndisse in nulla et tellus aliquet finibus.
4. Aliquam @quis diam in tortor euismod faucibus ac lobortis massa.

UPDATE

Turns out there were funny line endings that OS X didn't like. The following worked:

事实证明OS X不喜欢有趣的行结尾。以下工作:

tr '\r' '\n' < file.txt | grep -F '@' > newfile.txt

Thanks to @TomFenech for pointing me in the right direction.

感谢@TomFenech指出我正确的方向。

3 个解决方案

#1


It seems like your real problem was with your line endings but I think the best solution would be to use grep with the -F switch:

看起来你真正的问题在于你的行结尾,但我认为最好的解决方案是使用带-F开关的grep:

grep -F '@' file.txt

This prints lines that match the fixed string @ (no need for a regular expression match here).

这将打印与固定字符串@匹配的行(此处不需要正则表达式匹配)。

You can pipe the output of tr like this:

您可以像这样管道tr的输出:

tr -d '\r' < file.txt | grep -F '@'

Here I am deleting the carriage returns, rather than replacing them with a newline. Assuming that your files contain DOS-style line endings \r\n, this prevents blank lines from being added to the file.

这里我将删除回车符,而不是用换行符替换它们。假设您的文件包含DOS样式的行结尾\ r \ n,这可以防止将空行添加到文件中。

If your files only have carriage returns, then you can use this option to substitute them for newlines:

如果您的文件只有回车符,那么您可以使用此选项将它们替换为换行符:

tr '\r' '\n' < file.txt | grep -F '@'

#2


You can also say: hey, just print those lines containing @:

你也可以说:嘿,只需打印包含@的那些行:

$ sed -n '/@/p' file
1. Lorem ip@sum dolor sit amet, consectetur adipiscing elit.
3. Suspe@ndisse in nulla et tellus aliquet finibus.
4. Aliquam @quis diam in tortor euismod faucibus ac lobortis massa.

#3


you can do:

你可以做:

grep '@' file 

or with sed

或者与sed

sed -i '/@/!d' file

Edit

so your problem is, you added a space between sed's -i and ''. With this space, sed (tested with gnu sed) won't think the later '' is for backup. Also if you had -i option, you don't need the redirection. It will always give you an empty file.

所以你的问题是,你在sed的-i和''之间添加了一个空格。有了这个空间,sed(用gnu sed测试)不会认为后面的''用于备份。此外,如果您有-i选项,则不需要重定向。它总是会给你一个空文件。

#1


It seems like your real problem was with your line endings but I think the best solution would be to use grep with the -F switch:

看起来你真正的问题在于你的行结尾,但我认为最好的解决方案是使用带-F开关的grep:

grep -F '@' file.txt

This prints lines that match the fixed string @ (no need for a regular expression match here).

这将打印与固定字符串@匹配的行(此处不需要正则表达式匹配)。

You can pipe the output of tr like this:

您可以像这样管道tr的输出:

tr -d '\r' < file.txt | grep -F '@'

Here I am deleting the carriage returns, rather than replacing them with a newline. Assuming that your files contain DOS-style line endings \r\n, this prevents blank lines from being added to the file.

这里我将删除回车符,而不是用换行符替换它们。假设您的文件包含DOS样式的行结尾\ r \ n,这可以防止将空行添加到文件中。

If your files only have carriage returns, then you can use this option to substitute them for newlines:

如果您的文件只有回车符,那么您可以使用此选项将它们替换为换行符:

tr '\r' '\n' < file.txt | grep -F '@'

#2


You can also say: hey, just print those lines containing @:

你也可以说:嘿,只需打印包含@的那些行:

$ sed -n '/@/p' file
1. Lorem ip@sum dolor sit amet, consectetur adipiscing elit.
3. Suspe@ndisse in nulla et tellus aliquet finibus.
4. Aliquam @quis diam in tortor euismod faucibus ac lobortis massa.

#3


you can do:

你可以做:

grep '@' file 

or with sed

或者与sed

sed -i '/@/!d' file

Edit

so your problem is, you added a space between sed's -i and ''. With this space, sed (tested with gnu sed) won't think the later '' is for backup. Also if you had -i option, you don't need the redirection. It will always give you an empty file.

所以你的问题是,你在sed的-i和''之间添加了一个空格。有了这个空间,sed(用gnu sed测试)不会认为后面的''用于备份。此外,如果您有-i选项,则不需要重定向。它总是会给你一个空文件。