LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 06: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, 09:07 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,127

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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-01-2011, 11:37 PM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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, 11:27 AM   #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, 06:27 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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



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 06: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 10:31 PM
Using 'grep' to retrieve a function definition vdeych Programming 2 09-24-2008 04:28 AM
How can I retrieve kde default font? arshan Linux - General 3 08-11-2008 04:04 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:42 AM.

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
Open Source Consulting | Domain Registration