grep vs do while read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep vs do while read
# 1  
Old 01-29-2009
grep vs do while read

I have inherited the UNIX scripting duties for our team and have only been at this for a few months so I apologize for what may be a simple question.

We have a script we are reworking. Part of this script takes a parameter and reads a csv file. When it finds a matching line, the cut command is used to parse out and assign script variables from the matching line.

This works fine (code shown below). My question is really just a general one. I get the idea of using grep to "filter" just the records we want (based on $1, and then again, the lines that end with 'C')

In our case the file is very small no more than 25 lines. I am wondering if we could also use a do while read loop to actually "read" the file line by line instead of using grep - what would be the merits of each approach?

Any input you have would be greatly appreciated!

thanks,

JH




set RECORDS = `grep ^$1 ${CFGFILE} | grep 'C$'`

foreach record ( ${RECORDS} )
set rSID = `echo $record | tr -s " " | cut -f1 -d","`
set rCLIENT = `echo $record | tr -s " " | cut f2 -d","'
set rINSTANCE = `echo $record | tr -s " " | cut -f3 -d","`
set rSERVER = `echo $record | tr -s " " | cut -f4 -d","`
set rTYPE = `echo $record | tr -s " " | cut -f5 -d","`
# 2  
Old 01-29-2009
sh vs csh question

I have inherited the UNIX scripting duties on our team and have only been at this for few months. I apologize for what may be a basic question.


We have this snippet of code that gets lines from a file (using grep) based on a parameter we pass in (code shown below).

My question about this is that this code works when we execute it in a c-shell (#!/bin/csh) but not in bourne shell (#!/bin/sh).

I was wondering if there is a syntactical difference for this, and / or if there would be bourne shell equivalent.

Every script we have runs bourne shell, so I am wondering if there is any potential problems running this one as c-shell; better put, I'd be reluctant to run it as c-shell just for the sake of keeping this code.

Any info or direction would be greatly appreciated,

thanks

JH


set RECORDS = `grep ^$1 ${CFGFILE} | grep 'C$'`
foreach record ( ${RECORDS} )
set rSID = `echo $record | tr -s " " | cut -f1 -d","`
set rCLIENT = `echo $record | tr -s " " | cut f2 -d","'
set rINSTANCE = `echo $record | tr -s " " | cut -f3 -d","`
set rSERVER = `echo $record | tr -s " " | cut -f4 -d","`
set rTYPE = `echo $record | tr -s " " | cut -f5 -d","`
# 3  
Old 01-29-2009
Yes, csh is very different from sh, and you're doing well to avoid it.

Code:
RECORDS = `grep ^$1 ${CFGFILE} | grep 'C$'`
for record in ${RECORDS}
do
        rSID = `echo $record | tr -s " " | cut -f1 -d","`
        rCLIENT = `echo $record | tr -s " " | cut f2 -d","'
        rINSTANCE = `echo $record | tr -s " " | cut -f3 -d","`
        rSERVER = `echo $record | tr -s " " | cut -f4 -d","`
        rTYPE = `echo $record | tr -s " " | cut -f5 -d","` 
done

This is just a direct transliteration, as inelegant as it started as. There's probably a better way, avoiding the creation of 15 superfluous processes if we knew exactly what this script did.

Last edited by Corona688; 01-29-2009 at 01:32 PM..
# 4  
Old 01-29-2009
Quote:
Originally Posted by 15a0
I have inherited the UNIX scripting duties for our team and have only been at this for a few months so I apologize for what may be a simple question.

We have a script we are reworking. Part of this script takes a parameter and reads a csv file. When it finds a matching line, the cut command is used to parse out and assign script variables from the matching line.

This works fine (code shown below). My question is really just a general one. I get the idea of using grep to "filter" just the records we want (based on $1, and then again, the lines that end with 'C')

In our case the file is very small no more than 25 lines. I am wondering if we could also use a do while read loop to actually "read" the file line by line instead of using grep - what would be the merits of each approach?

Any input you have would be greatly appreciated!

thanks,

JH




set RECORDS = `grep ^$1 ${CFGFILE} | grep 'C$'`

foreach record ( ${RECORDS} )
set rSID = `echo $record | tr -s " " | cut -f1 -d","`
set rCLIENT = `echo $record | tr -s " " | cut f2 -d","'
set rINSTANCE = `echo $record | tr -s " " | cut -f3 -d","`
set rSERVER = `echo $record | tr -s " " | cut -f4 -d","`
set rTYPE = `echo $record | tr -s " " | cut -f5 -d","`
reading in a loop would allow you to process an arbitrary number of records without worrying about the maximum size of variable your shell allows. Note my code is in sh, not csh:
Code:
grep ^$1 ${CFGFILE} | grep 'C$' | while read RECORD
do
...
done

Those two grep processes could probably be combined into one if you used egrep instead:
Code:
egrep "^$1.*C$" | while read RECORD
do
...
done

But these pipes do something potentially unexpected, insulating themselves from variables outside the loop:

Code:
VAR=s

echo 'a b c ' | while read -d ' ' Q
do
        echo $Q
        VAR=$Q
done

echo "VAR is $VAR"

Because the loop behind the pipe is a seperate process, variable changes in it are not reflected outside it, hence VAR remains S.
# 5  
Old 01-29-2009
sh's built-in string splitting ability makes a lot of that csh code redundant, in fact, I wonder if this works properly with your data:
Code:
IFS=","
egrep "^$1.*C$" | tr -s ' ' | while read rSID rCLIENT rINSTANCE rSERVER rTYPE
do
        echo "rSID = $rSID"
        echo "rCLIENT = $rCLIENT"
        echo "rINSTANCE = $rINSTANCE"
        echo "rSERVER = $rSERVER"
        echo "rTYPE = $rTYPE"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read each file grep a value from that?

Hi Team, in /tmp folder i have thousands of log files i want to read each file and grep a value called "Calling This". Each logfile name is different but it ends with .log. How can i achieve this? Please excuse if i did any mistake by not following forum standards. I will surely follow... (10 Replies)
Discussion started by: darling
10 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

How to grep and count the occurences in while read script?

Hi, I have a file while is the output of lspath command and output of file is #cat lspath.txt Enabled hdisk0 vscsi0 Enabled hdisk0 vscsi1 Enabled hdisk1 vscsi0 Enabled hdisk2 vscsi0 Enabled hdisk2 vscsi1 Missing hdisk3 vscsi0 Enabled hdisk3 vscsi1 Have created script to check state... (7 Replies)
Discussion started by: ksgnathan
7 Replies

4. Shell Programming and Scripting

read from file and grep using shell

Hi Guys, I have a small script which greps for the username reading from stdinput. ./file.sh pattern pattern=$1 grep "blah blah.*$pattern" /home/user/log.txt Instead of typing the pattern everytime i want to read the pattern from a file inside the shell script and execute the... (5 Replies)
Discussion started by: Irishboy24
5 Replies

5. UNIX for Dummies Questions & Answers

Strange error: grep does not read from file

I have two files file1.txt angie mary susan file2.txt angie blond mary brunnet susan red christine blackI want to get this output angie blond mary brunnet susan redI write grep --file=file1.txt file2.txtand i get no results i also wrote cat file1.txt|while read line... (19 Replies)
Discussion started by: FelipeAd
19 Replies

6. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

7. Shell Programming and Scripting

read the first 10 lines below a keyword found by grep

Hello Everyone, i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword. example file.txt ------------------------------------------------------------------ --header this is the... (7 Replies)
Discussion started by: alain.kazan
7 Replies

8. Shell Programming and Scripting

grep within while read loop

I have a key file $ cat klist 5 N:8855 CASA VERDE ROAD :32827 :ORLAND 5 N:585 MOLLY LANE :30189 :WOODST 5 N:320 NINA ROAD :32304 :TALLAH and a data file, see example of the line below: N:RT 15 & N 7TH STREET :17837 :U SAVE I need to search by key (2nd field) from klist... (6 Replies)
Discussion started by: migurus
6 Replies

9. UNIX for Dummies Questions & Answers

Using the Read command with grep

I am still fairly new to unix scripting and I will try my best to explain my question the best I can.... I have a text file called hello.txt, and in this file I need to search every line that contains the words "help" and "cat" and move the respective lines into their own columns in another... (2 Replies)
Discussion started by: silvermax
2 Replies

10. Shell Programming and Scripting

Read file then grep the line

Dear all, I am reading a file that has 1 column. While reading I must find the line references from the another file. The following shell doesn't works. Please help #!/bin/bash while read filename; do grep ${filename} fs_full.dat >> unprocfull.dat; done < unproc.dat But when... (2 Replies)
Discussion started by: mr_bold
2 Replies
Login or Register to Ask a Question