简体   繁体   English

linux命令行文本文件操作

[英]linux command line text file manipulation

Let's say I have a text file with the following contents: 假设我有一个包含以下内容的文本文件:

test
blablabla
test1
moreblabla
atest
blabla3
just*a*test
blabla14
test88
lastblabla

I want to delete ONLY the line that matches EXACTLY the string "test" and the line after it WITHOUT deleting the lines that contain the string "test" So in my case I want to delete just the first and second lines but not the rest. 我想删除完全匹配字符串“测试”并线后,而不删除包含字符串“测试”所以在我的情况下,我想删除就在第一和第二线,但不休息的线的线条。

The above file is just an example, the contents of the file varies just like the string. 上面的文件只是一个例子,文件的内容就像字符串一样。

All the examples I found with sed only deal with deleting lines that just contain some string. 我在sed找到的所有示例仅涉及删除仅包含一些字符串的行。 I want to delete only the line that IS exactly that string. 我想只删除正是串线。

Just add ^ and $ around the test regex to anchor your match. 只需在test正则表达式周围添加^$即可锚定您的匹配项。

sed -e '/^test$/,+1d' path/to/file

Or you could do ^ *test *$ for a line containing 'test' possibly with spaces. 或者,您可以对包含“ test”(可能带有空格)的行执行^ *test *$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM