使用grep -e检索默认值?

时间:2022-10-01 08:54:24

I am parsing text files looking for specific entries like so

我正在解析文本文件,寻找这样的特定条目

grep -e 'Model' -e 'Manufacturer' -e 'Man Date' -e 'SW Version' -e' SW Name' -e 'HW Version' -e 'Receiver ID' JGMDTV356.HDD

This gives me an output like so

这给了我一个像这样的输出

Model         = HR24
Manufacturer  = 100
Man Date      = 04/14/2010
SW Version    = 4D1
HW Version    = 2.3
Receiver ID   = 035635905389
Model         = WDCWD5000AVVS-63M8B0 (Dragonfly-0)

The problem is that some files do not have the same number of fields. How can I do something like this?

问题是有些文件的字段数不同。我该怎么做呢?

Model         = HR24
Manufacturer  = 100
Man Date      = N/A
SW Version    = 4D1
HW Version    = N/A
Receiver ID   = N/A
Model         = N/A

Could this be done?

这有可能吗?

2 个解决方案

#1


1  

As Matthew mentioned, what you are trying to do is beyond the intended use of grep. Awk was made for this type of thing and is available on most linux distros (gawk is the GNU implementation) and even many embedded Linux systems. There is a good tutorial for it here that will take under an hour and teach you pretty much all you need to know.

正如Matthew提到的,您所要做的超出了grep的预期用途。Awk是为这种类型的东西而设计的,并且可以在大多数linux发行版(gawk是GNU的实现)甚至许多嵌入式linux系统上使用。这里有一个很好的教程,在不到一个小时的时间里,你就能学到你需要的所有知识。

Here is the awk code you will need to solve your problem:

这是你需要解决的awk代码:

#!/bin/awk -f                                                                                             

BEGIN {
    tokens["Model"] = 0
    tokens["Manufacturer"] = 0
    tokens["Man Date"] = 0
    tokens["SW Version"] = 0
    tokens["HW Version"] = 0
    tokens["Receiver ID"] = 0
}

{
    for (token in tokens){
        if ($1 == token){
            print $0;
            tokens[$1]++;
        }
    }
}

END {
    for (token in tokens){
        if( tokens[token] == 0){
            printf("%-13s = NA\n" , token)
        }
    }
}

Save this to a tmp.awk and add execute permissions to tmp.awk.

将其保存到tmp。awk并向tmp.awk添加执行权限。

cat <your file> | ./tmp.awk

cat <您的文件> | ./tmp.awk

This will print out what you want.

这将打印出你想要的。

#2


1  

So, if file JGMDTV356.HDD doesn't contain any line matching pattern FOO, you want grep to print out FOO = N/A? grep can't do that. You might want to take a look at gawk for text file processing that's more complicated than what grep can handle.

所以,如果文件JGMDTV356。HDD不包含任何行匹配模式FOO,您希望grep输出FOO = N/A吗?grep不能那么做。您可能想看看gawk的文本文件处理,它比grep能处理的更复杂。

#1


1  

As Matthew mentioned, what you are trying to do is beyond the intended use of grep. Awk was made for this type of thing and is available on most linux distros (gawk is the GNU implementation) and even many embedded Linux systems. There is a good tutorial for it here that will take under an hour and teach you pretty much all you need to know.

正如Matthew提到的,您所要做的超出了grep的预期用途。Awk是为这种类型的东西而设计的,并且可以在大多数linux发行版(gawk是GNU的实现)甚至许多嵌入式linux系统上使用。这里有一个很好的教程,在不到一个小时的时间里,你就能学到你需要的所有知识。

Here is the awk code you will need to solve your problem:

这是你需要解决的awk代码:

#!/bin/awk -f                                                                                             

BEGIN {
    tokens["Model"] = 0
    tokens["Manufacturer"] = 0
    tokens["Man Date"] = 0
    tokens["SW Version"] = 0
    tokens["HW Version"] = 0
    tokens["Receiver ID"] = 0
}

{
    for (token in tokens){
        if ($1 == token){
            print $0;
            tokens[$1]++;
        }
    }
}

END {
    for (token in tokens){
        if( tokens[token] == 0){
            printf("%-13s = NA\n" , token)
        }
    }
}

Save this to a tmp.awk and add execute permissions to tmp.awk.

将其保存到tmp。awk并向tmp.awk添加执行权限。

cat <your file> | ./tmp.awk

cat <您的文件> | ./tmp.awk

This will print out what you want.

这将打印出你想要的。

#2


1  

So, if file JGMDTV356.HDD doesn't contain any line matching pattern FOO, you want grep to print out FOO = N/A? grep can't do that. You might want to take a look at gawk for text file processing that's more complicated than what grep can handle.

所以,如果文件JGMDTV356。HDD不包含任何行匹配模式FOO,您希望grep输出FOO = N/A吗?grep不能那么做。您可能想看看gawk的文本文件处理,它比grep能处理的更复杂。