Bash 正则表达式提取内容:使用 grep 和 sed 命令
可以使用 'grep' 和 'sed' 命令来提取内容。
使用 'grep' 命令:
grep -o '正则表达式' 文件名
示例:
假设有一个文件 'test.txt',内容如下:
hello world
foo bar
123 456
abc def
如果想提取以字母开头的行,可以使用以下命令:
grep -o '^[a-zA-Z]*' test.txt
输出结果为:
hello
foo
abc
使用 'sed' 命令:
sed -n '/正则表达式/p' 文件名
示例:
假设有一个文件 'test.txt',内容如下:
hello world
foo bar
123 456
abc def
如果想提取以字母开头的行,可以使用以下命令:
sed -n '/^[a-zA-Z]*/p' test.txt
输出结果为:
hello world
foo bar
abc def
原文地址: https://www.cveoy.top/t/topic/mEFt 著作权归作者所有。请勿转载和采集!