question about grep, cut, and piping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting question about grep, cut, and piping
# 1  
Old 02-28-2008
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  
Old 02-28-2008
Java

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Piping to grep with pbpaste

cat file 1 aaa 2 bbb 3 ccc 4 ddd In TextEdit, I then copy the characters “ccc” to the clipboard. The problem is that the following command gives no output: bash-3.2$ pbpaste | grep - file Desired output: 3 ccc What should the syntax be for that command? I am using MacOS El... (3 Replies)
Discussion started by: palex
3 Replies

2. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

3. Shell Programming and Scripting

piping from grep to awk without intermediate files

I am trying to extract the file names alone, for example "TVLI_STATS_NRT_XLSTWS03_20120215_132629.csv", from below output which was given by the grep. sam:/data/log: grep "C10_Subscribe.000|subscribe|newfile|" PDEWG511_TVLI_JOB_STATS.ksh.201202* Output: ... (6 Replies)
Discussion started by: siteregsam
6 Replies

4. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (6 Replies)
Discussion started by: frymor
6 Replies

5. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (1 Reply)
Discussion started by: frymor
1 Replies

6. UNIX for Dummies Questions & Answers

Piping GREP

Hi, I need to use a double grep so to speak. I need to grep for a particular item say BOB and then for each successful result I need to grep for another item say SMITH. I tried grep "BOB" filename | grep "SMITH" but it does not seem to work. I can achieve my desired result using an... (12 Replies)
Discussion started by: mojoman
12 Replies

7. UNIX for Dummies Questions & Answers

Piping syntax question

There are are lots of examples of piping output FROM the 'ls' command TO another command, but how does one pipe output TO the 'ls -l' command? For example, use 'which' to find a file, then use 'ls -l' to view the permissions, groups, etc. in a single step: which <filename> | ls -l returns... (4 Replies)
Discussion started by: johne1
4 Replies

8. Programming

Piping Question

I have a piping question, I am trying to implement piping on my own shell and am having some trouble...esentially I am trying to make something to do command|command|command. I can get it to work fine if the last pipe command is not forked, but executes in the shell and then exits..but I need it... (2 Replies)
Discussion started by: mtobin1987
2 Replies

9. UNIX for Dummies Questions & Answers

piping the output of find command to grep

Hi, I did not understand why the following did not work out as I expected: find . -name "pqp.txt" | grep -v "Permission" I thought I would be able to catch whichever paths containing my pqp.txt file without receiving the display of messages such as "find: cannot access... Permisson... (1 Reply)
Discussion started by: 435 Gavea
1 Replies

10. Shell Programming and Scripting

Help (Piping ls, tr, cut)

I have to: pipe ls, tr, and cut to output the size (in bytes) and name of all of the files/dirs in the current directory (including any hidden ones), with the size at the beginning of the line, followed by a single tab character, followed by the the filename. I don't know what the point of... (2 Replies)
Discussion started by: scan
2 Replies
Login or Register to Ask a Question