indexing a file

hello guys,

I have a file like this:

input.dat

Push-to-talk 
No
Coonection
IP support 
Support for IP telephony 
Yes 
Built-in SIP stack 
Yes 
Support via software 
Yes 
Microsoft
Support for Microsoft Exchange 
Yes 
UMA 
No 
Internet
Internet connection
WAP 
Yes 
Weight 
129 g 
GUI type
Display 
Colour 
Number of colours 
262144 
Display technology 
LCD
Display Resolution
Screen resolution  
320x480 pixels  
Screen size  
3.2 inches  
Touchscreen  
Yes  
Simultaneous touch points  
3+ Multi-touch  
Type of technology  
Capacitive

and the feature list is:

featureList.dat

Speaker
Support for IP telephony 
Support for Microsoft Exchange 
UMA 
WAP 
Weight 
Bluetooth
Dimension
Display technology 
Screen resolution  
Screen size  
Touchscreen  
Type of technology
USB connsection

and the desired output is:

output.dat:

null
Yes
Yes
No
Yes
129 g
null
null
LCD 
320x480 pixels
3.2 inches
Yes
Capacitive
null

I want to see what are the values of the features in the input file (features have their values in the line below them)

if the feature does not exist then the value would be "null"

there are some features in the input that are not in the featureList.dat, so just the desired features are the subject for the extraction.

I came up to this code by the help of Ahamed, but I have problem with it :frowning:

awk '{gsub(/[ ]*$/,"")}NR==FNR{x=$0;getline;getline;a[x]=$0;next}{print a[$0]?a[$0]:"null"}'  file1 file2

Okay, but what problem do you have?

if I have some extra lines between features like the input file above, all of the output lines would be "null" :frowning:

---------- Post updated at 06:18 PM ---------- Previous update was at 05:31 PM ----------

do you have any other idea?

Try this...

#!/bin/bash
while read line
do
        val=$( grep -A1 "$line" file1 | tail -1 )
        echo ${val:-null}
done < file2

In case your grep doesn't support -A option, then replace the grep line with any of these

val=$( sed -n "/$line/{n;p;q}" file1 )
#or
val=$( awk '{if($0~srch){getline;print;exit}}' srch="$line" file1 )

HTH
--ahamed

1 Like

hey ahamed,

thnx, it worked, but didn't show the last null, I don't know why!

I am getting it...

root@bt:/tmp# ./run
null
Yes
Yes
No
Yes
129 g
null
null
LCD
320x480 pixels
3.2 inches
Yes
Capacitive
null

--ahamed