sed regex命令输出以html结尾的行?

时间:2019-10-18 00:51:05

标签: regex linux bash unix sed

我需要一个sed regex命令,该命令会在一段时间内输出以“ html”结尾且不以“ a”开头的每一行。

我当前的代码可以工作吗?

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream outfile;
    outfile.open("afile.dat");

    float a[6];

    for (int i=0;i<6; i++){
        outfile <<a[i]<<endl;
    }

    outfile.close();
    return 0;
}

3 个答案:

答案 0 :(得分:2)

sed命令应为

sed -n '/^[^a].*html$/p'

但是打印匹配行的规范命令是grep

grep '^[^a].*html$'

答案 1 :(得分:0)

Sed刚刚使事情变得复杂...您可以使用grep轻松地处理它!

egrep "^[^a].+\.html$" -f sourcefile > text.txt
//loads file from within the program egrep

egrep "^[^a].+\.html$" < sourcefile > text.txt
//Converts stdin file descriptor with the input redirect 
//to sourceFile for this stage of the` pipeline 

在功能上等效。

pipe input |  xargs -n1 egrep "^[^a].+\.html$" > text.txt

//xargs  -n1 means take the stdin from the pipe and read it one line at a time in conjunction with the single command specified after any other xargs arguments
// ^ means from start of line, 
//. means any one character
//+ means the previous matched expression(which can be a 
//(pattern group)\1 or [r-ange], etc) one or more times
//\. means escape the single character match and match the period character

//$ means end of line(new line character)
//egrep is short for extended regular expression matches which are really 

好 (假设您没有使用烟斗或猫等)

您可以使用以下命令将换行符分隔的文件转换为单个输入行:

cat file | tr -d '\n' ' '
//It converts all newlines into a space!

无论如何,通过简单的实用程序来发挥创造力,您可以做很多事情:

xargs,grep,tr是易于学习的好组合。没有这一切的静

答案 2 :(得分:0)

请勿对sed执行此操作。用两个不同的grep调用来完成

grep -v ^a file.txt | grep 'html$'

第一个grep获取所有不以“ a”开头的行,并将其输出发送到第二个grep,第二个grep提取所有以“ html”结尾的行。

相关问题