back to basics with GREP and FOR


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting back to basics with GREP and FOR
# 1  
Old 06-02-2006
back to basics with GREP and FOR

Hi,

consider this data file - data.dat - with the below contents
this is a test
this is no test
this is also a test


the below script -
for i in `grep a data.dat`
do
echo $i
done


will produce the output --
this
is
a
test
this
is
also
a
test


However, if we use " around the `grep ....`
for i in "`grep a data.dat`"
do
echo $i
done


would give the results as --
this is a test this is also a test

What needs to be done to the for script in order to get the desired output -
this is a test
this is also a test
# 2  
Old 06-02-2006
Is this your exact requirement? If you just want to print the lines then you can very well say followingin your script
#!/bin/ksh
grep a data.dat

If you want to do some processing then you can write the o/p to file and process each line.

hope this helps
# 3  
Old 06-02-2006
I will be doing processing ...

for i in `grep ......`
do
for j in `grep $i .......`
do
......
......
done
done
# 4  
Old 06-03-2006
#!/bin/ksh
FILENAME="$1"
grep a $FILENAME | while read LINE
do
echo "$LINE"
done
# 5  
Old 06-03-2006
Thanks Sheetal. I was able to proceed to the next level.
Let me re-phrase the script.

file name: data
> cat data
1|one|this is also one|a
2|one|this is also one|b
3|one|this is also one|c
4|one|this is also one|d


modified script
#!/bin/ksh
FILENAME="$1"
grep one $FILENAME | cut -d'|' -f3 | while read LINE
do
echo "-----" "$LINE" "-----" #### The $LINE is "this is also one" .. has embedded space chars
for i in `grep $LINE $FILENAME | cut -d'|' -f2`
do
echo "****" $i "****"
done
done


Output --
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.

**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.

**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.

**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.

**** one ****
**** one ****
**** one ****
**** one ****


Desired output
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
# 6  
Old 06-03-2006
In your code,
echo "-----" "$LINE" "-----"
Remove the double quotes around $LINE. I think it should work.
# 7  
Old 06-03-2006
The problem is not with the
echo "-----" $LINE "-----"

but, it is in the
for i in `grep $LINE $FILENAME | cut -d'|' -f2`

The reason, the pattern in $LINE has embedded space char in it. So, when grep reads the $LINE, like --
grep this is also one $FILENAME | cut -d'|' -f2
thats where it fails.

The question is how to apply QUOTES around $LINE for grep, so that when grep expands it, it should do it like --
grep "this is also one" $FILENAME | cut -d'|' -f2

thanks,
s.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX basics

Hi, I am new to Unix. can you explain in brief with examples what is variable, what is argument and what is parameter? i searched a lot on other forums but not able to find a appropriate answer. thanks in advance!! (3 Replies)
Discussion started by: 21laps
3 Replies

2. UNIX for Dummies Questions & Answers

help me with basics

hello everyone i have to start with unix as it is a part of my training programme and i have to do a self study, i dont know where to start from. i need some basic questions to be answerd like why we use unix ? what is a terminal? what is an editor? why we write commands inside terminal? these... (4 Replies)
Discussion started by: aryancool
4 Replies

3. AIX

AIX Basics

Hello , Everyone , I want to know the Aix Basics and how it works ,hardware related problems and solution etc. (1 Reply)
Discussion started by: narendram
1 Replies

4. IP Networking

Back-to-Back Connection using HBAs

Hi every body, Is it possible to connect two servers Back-to-Back (Point-to-Point) using HBA adapters & using Fiber. Note it is direct connection & there is no switches between the servers. I'm concern about using HBA adapters, it is possible or not. Thanks in advance. :) (3 Replies)
Discussion started by: aldowsary
3 Replies

5. Shell Programming and Scripting

Scripts and basics

Hi, I have a script which I need to modify the date "01/10/2008" and "31/10/2008" every month before running the report. Can I have some script to take the date automatically from the cal option. Hope it makes sense. Thanks in advance. (1 Reply)
Discussion started by: Jayanthsec
1 Replies

6. AIX

back to back printing in UNIX

Hi , Can you suggest me how to back to back printing in UNIX? Is there any way? Kindly advise. Regards Vijaya Amirtha Raj (3 Replies)
Discussion started by: amirthraj_12
3 Replies

7. IP Networking

SAN basics

Hi I like to learn and practice SAN, iSCSI. Could you sugges the appropriate tutorial and small tasks to practice SAN. Thankyou (1 Reply)
Discussion started by: kingskar
1 Replies
Login or Register to Ask a Question