LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Retrieve default value with grep -e? (https://www.linuxquestions.org/questions/linux-newbie-8/retrieve-default-value-with-grep-e-911361/)

kristo5747 11-01-2011 06:55 PM

Retrieve default value with grep -e?
 
I am parsing text files looking for specific entries like so

Code:

    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

Code:

    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?

Code:

  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?

syg00 11-01-2011 09:07 PM

Not (easily) with grep - anything with associative arrays (bash, awk, perl, ...) you should be able to do something. Maybe populate all as "n/a" and only print the input if found, else the default. If you don't want to hard-code the fields, make up a file containing them, and read it in.

allend 11-01-2011 11:37 PM

An ugly hack using a bash script assuming that Model is always present and comes first and the other fields come in the same order if present.
Code:

#! /bin/bash

while read line
do
  if [[ $line =~ Model.* ]]; then
    echo $line
    read line
    for i in "Manufacturer" "Man Date" "SW Version" "HW Version" "Receiver ID"
    do
      if [[ $line =~ $i.* ]]; then
        echo $line
        if [ "$i" != "Receiver ID" ]; then
          read line
        fi
      else
        echo $i" = N/A"
      fi
    done
  fi
done < test.txt

If test.txt contains:
Code:

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

then the ouput is:
Quote:

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

kristo5747 11-02-2011 11:27 AM

Quote:

Originally Posted by allend (Post 4513805)
An ugly hack using a bash script assuming that Model is always present and comes first and the other fields come in the same order if present.
Code:

#! /bin/bash

while read line
do
  if [[ $line =~ Model.* ]]; then
    echo $line
    read line
    for i in "Manufacturer" "Man Date" "SW Version" "HW Version" "Receiver ID"
    do
      if [[ $line =~ $i.* ]]; then
        echo $line
        if [ "$i" != "Receiver ID" ]; then
          read line
        fi
      else
        echo $i" = N/A"
      fi
    done
  fi
done < test.txt

If test.txt contains:
Code:

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

then the ouput is:


Ugly maybe, but this is a clever hack.

Unfortunately, this won't fly. Depending on the supplier that sends the file, Model may not even be present.

I need to devise a strategy where the relevant stuff is captured and the rest is rejected.

Thanks for your time.

chrism01 11-02-2011 06:27 PM

In that case, I concur with syg00, grep is not the tool here, you need a proper prog lang (in my case Perl) that can handle arbitrary data layouts that may have missing elements.


All times are GMT -5. The time now is 05:58 PM.