简体   繁体   English

Bash删除所有以空格开头的行

[英]Bash remove all lines starting with a string including whitespaces

I'm trying to delete a few lines from fstab using sed or awk, i want to delete those lines that are starting with some prefix, those lines might have whites spaces. 我正在尝试使用sed或awk从fstab中删除几行,我想删除以某些前缀开头的行,这些行可能带有空格。

I've tried this option: sed -i '/^SomeFileSystem/d' fstab but it's not deleting the line.. 我试过这个选项: sed -i '/^SomeFileSystem/d' fstab但它没有删除该行。

fstab entry: fstab条目:

myServer:/clearcase /clearcase nfs rw,hard,intr,bg,tcp,vers=3,rsize=32768,wsize=32768,timeo=4

This is the command that I'm trying to execute 这是我要执行的命令

sed -i '/^myServer:/clearcase/d' fstab

This is probably not working because of the slash / in the string you are looking for. 由于您要查找的字符串中的斜杠/ ,因此此方法可能无法正常工作。

Just use another sed separator: 只需使用另一个sed分隔符即可:

sed -i '\@^myServer:/clearcase@d' fstab

Escape the slash inside: 转义内部的斜线:

myServer:/clearcase /clearcase nfs rw,hard,intr,bg,tcp,vers=3,rsize=32768,wsize=32768,timeo=4

Try this: 尝试这个:

sed -i '/^myServer:\/clearcase/d' fstab

If the line may start with whitespace, adapt your regex to say so. 如果该行可能以空格开头,请修改您的正则表达式。

sed -i '\#^ *myserver:/clearcase#d' fstab

This also avoids the requirement to escape the slash by using a different regex delimiter than slash. 这也避免了使用与斜杠不同的正则表达式定界符来避免斜杠转义的要求。

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

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