The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 02-28-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Post

#!/bin/sh file=$1 field=`echo $2 | cut -d '=' -f 1` value=`echo $2 | cut -d '=' -f 2` fields=`head -1 $file` fieldnum=0 thisfield="" while [ "$thisfield" != "$field" ] do fieldnum=`expr $fieldnum + 1` thisfield=`echo $fields | cut -d ',' -f $fieldnum` done tail +2 $file | while read line do checkfield=`echo $line | cut -d ',' -f $fieldnum` if [ "$checkfield" = "$value" ] then echo $line fi done
-->
Code:
#!/bin/sh
file=$1
field=`echo $2 | cut -d '=' -f 1`
value=`echo $2 | cut -d '=' -f 2`
fields=`head -1 $file`

fieldnum=0
thisfield=""
while [ "$thisfield" != "$field" ]
do
  fieldnum=`expr $fieldnum + 1`
  thisfield=`echo $fields | cut -d ',' -f $fieldnum`
done

tail +2 $file | while read line
do
  checkfield=`echo $line | cut -d ',' -f $fieldnum`
  if [ "$checkfield" = "$value" ]
  then
    echo $line
  fi
done
        
Untested
Usage:
scriptname.sh filename '"fielname"="value"'

Note the single ticks to protect the double quotes.

You could probably add some smarts to prevent it parsing the " symbols at all, making the whole thing a bit less clunky.