Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve default value with grep -e?

Status
Not open for further replies.

kristo5747

Programmer
Mar 16, 2011
41
US
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?

 
If they blocks all start w/ model, you can try:

Code:
egrep -p Model < [input_file]

It should return the entire paragraph to the next blank line.
 
To do that you really need to handle the expected fields explicitly. awk or perl are probably best for the job, try this solution perhaps:


Code:
awk -F '[green] *= *' [/green]'
        [green]BEGIN[/green] {
                model1=model2=manuf=mandate=swver=hwver=recid=[red]"[/red][purple]N/A[/purple][red]"[/red]
        }
        [green]/^    Model/[/green] && model1==[red]"[/red][purple]N/A[/purple][red]"[/red] { model1=[blue]$2[/blue]; [b]next[/b] }
        [green]/^    Manufacturer/[/green] { manuf=[blue]$2[/blue]; [b]next[/b] }
        [green]/^    Man Date/[/green] { mandate=[blue]$2[/blue]; [b]next[/b] }
        [green]/^    SW Version/[/green] { swver=[blue]$2[/blue]; [b]next[/b] }
        [green]/^    HW Version/[/green] { hwver=[blue]$2[/blue]; [b]next[/b] }
        [green]/^    Receiver ID/[/green] { recid=[blue]$2[/blue]; [b]next[/b] }
        [green]/^    Model/[/green] { model2=[blue]$2[/blue]; [b]next[/b] }
        [green]/^[[:space:]]*$/[/green] && model1 != [red]"[/red][purple]N/A[/purple][red]"[/red] {

                [b]print[/b] [red]"[/red][purple]    Model         = [/purple][red]"[/red]model1
                [b]print[/b] [red]"[/red][purple]    Manufacturer  = [/purple][red]"[/red]manuf
                [b]print[/b] [red]"[/red][purple]    Man Date      = [/purple][red]"[/red]mandate
                [b]print[/b] [red]"[/red][purple]    SW Version    = [/purple][red]"[/red]swver
                [b]print[/b] [red]"[/red][purple]    HW Version    = [/purple][red]"[/red]hwver
                [b]print[/b] [red]"[/red][purple]    Receiver ID   = [/purple][red]"[/red]recid
                [b]print[/b] [red]"[/red][purple]    Model         = [/purple][red]"[/red]model2
                [b]print[/b] [red]"[/red][purple][/purple][red]"[/red]

                model1=model2=manuf=mandate=swver=hwver=recid=[red]"[/red][purple]N/A[/purple][red]"[/red]
        }

' inputfile

It assumes an empty lines between records and at the end of the input data, because it uses that to trigger the printing of each record and resetting the values for the next one.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top