LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2011, 07:55 PM   #1
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Rep: Reputation: 0
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?
 
Old 11-01-2011, 10:07 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
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.
 
Old 11-02-2011, 12:37 AM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
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
 
Old 11-02-2011, 12:27 PM   #4
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by allend View Post
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.
 
Old 11-02-2011, 07:27 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] storing grep output to feed awk to retrieve entire records matching variable chargaff Programming 8 08-13-2010 07:10 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
Using 'grep' to retrieve a function definition vdeych Programming 2 09-24-2008 05:28 AM
How can I retrieve kde default font? arshan Linux - General 3 08-11-2008 05:04 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 06:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration