The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Piping Question mtobin1987 High Level Programming 2 04-25-2008 11:24 PM
Grep question. saurabhsinha23 UNIX for Dummies Questions & Answers 3 12-11-2007 01:58 AM
piping the output of find command to grep 435 Gavea UNIX for Dummies Questions & Answers 1 10-05-2006 04:48 PM
Grep Question nazri76 UNIX for Dummies Questions & Answers 3 08-18-2006 09:40 AM
grep question raje17 Shell Programming and Scripting 3 04-11-2006 07:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-28-2008
dyrt dyrt is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 1
question about grep, cut, and piping

Howdy folks,

I am fairly new to scripting but have lost of expirience in c++, pascal, and a few other. I am trying to complete a file search script that is sent a file name containing data to search that is arranged like this

"id","name","rating"
"1","bob","7"
etc

and an argument to search such as name=bob. My problem is that id and rating are both numerical so a simple grep won't work, so i need to search the correct field for the correct data. i need to then run a script(which i have already written and tested to work) that will accept the line numbers and the file name to print the lines that match. It is there where i am stuck.

Any suggestions how i can correctly print only the lines where the correct field matches? i can just forget the print lines script if there is an easier way that i missed. thanks for any help!

Mike
  #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: 921
Post

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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0