problem using a pipe to grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem using a pipe to grep
# 1  
Old 05-31-2009
problem using a pipe to grep

Hello !

I want to process a text file in order to extract desired data using sed and grep... Now I am facing a problem piping to grep... nothing happens..
The text consists of blocks of 3 lines that may (or not) contain the Desired data.

Quote:
line11 blabla1
line12 blabla2..
line13 blabla3..
line21 blabla1.. DesiredInfo
line22 blabla2..
line23 blabla3..
line31 blabla1.. DesiredInfo
line32 blabla2..
line33 blabla3..
etc.
the desired data is on each 2 first lines of some 'blocks'.. so I started by using the following so as to join the desired 2 lines in 1 (and skip the 3rd) in order to use it after with grep/sed :

Code:
#!/bin/bash
#script.sh
(while read line;
do
    read line2;
    read line3;
    echo $line $line2;
done)

Now I want to select only the lines containing the "DesiredInfo", for that I thought simply of adding a pipe to grep after the preceding code:

Code:
| grep DesiredInfo

but.. it doesn't work like that...(nothing happens) so I thought of putting directly in the shell prompt instead of the script:

Quote:
bash script.sh < directory/file.txt | grep DesiredInfo
but then again, nothing happens...
Is there a problem with piping to 'grep' ?

thank you for your precious help !!

Last edited by ShellBeginner; 05-31-2009 at 10:02 AM..
# 2  
Old 05-31-2009
You have an open bracket "(" but no close bracket anywhere, you need to drop the open bracket as a first step to getting this working.
# 3  
Old 05-31-2009
Thank you for answering me...

Actually I found that the 'problem' was that it took so much time (50 sec.) to display the result that I thought it was not working... but in fact it was ok
SmilieSmilieSmilie

Thank you anyway !! Smilie
# 4  
Old 05-31-2009
then try using simple awk instead of while loop
Code:
awk '{var=$0}{getline}{var2=$0}{getline}{var3=$0}{print var" "var2}' filename|grep DesiredInfo

# 5  
Old 05-31-2009
Quote:
Originally Posted by vidyadhar85
then try using simple awk instead of while loop
Code:
awk '{var=$0}{getline}{var2=$0}{getline}{var3=$0}{print var" "var2}' filename|grep DesiredInfo


Oh ! thank you ! really efficient with that !
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep:pipe delimited output

Hi, I am trying to search for a string in a file and print all the matched lines as pipe delimited format. My command is cat m_gid_trans.XML|grep -i '<TABLEATTRIBUTE NAME ="Lookup cache directory name"' The output I am getting is <TABLEATTRIBUTE NAME ="Lookup cache directory name"... (4 Replies)
Discussion started by: sampoorna
4 Replies

2. UNIX for Dummies Questions & Answers

How To Pipe Find To Grep?

hi what wrong with my syntax here? find / -name "*dhcp*" | grep -l -r 'timeout' thanks (3 Replies)
Discussion started by: johnywhy
3 Replies

3. Shell Programming and Scripting

pipe search pattern into a grep

comm -13 tmpfile tmpfile2 | grep -v <filename> >newfile so i want to 1. find records in 1 file bot not in another 2. The output of the first part is 1 field in a file with many fields. 3. find all the records that do not have the value piped from step #1 4. redirect to a new file ... (4 Replies)
Discussion started by: guessingo
4 Replies

4. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

5. UNIX for Dummies Questions & Answers

Pipe grep to rm

We have a number of OS X 10.6 machines who have home folders with incorrect permissions, which are causing managed prefs not to be applied correctly and not allowing saving. I need to delete all home folders whose name is numerical and modified before a certain date. I'm not sure of the date part... (2 Replies)
Discussion started by: airlocksniffer
2 Replies

6. Shell Programming and Scripting

pipe result from grep

Trying to create a command line script to look for all files matching a pattern, grep for a specific value in each file, and write out the filename long list. It's possible the filename won't containe the value. { echo “Running....” for fname in 811_Intermediate_File_* do grep -l... (3 Replies)
Discussion started by: gavineq
3 Replies

7. UNIX Desktop Questions & Answers

How to grep and pipe to mput ftp

I am ptting the following into an FTOP script. date=`TZ="aaa24" date +'%Y%m%d'` # this gets Yesterday date . . . . mput < 'l | grep $Vdate' # the idea to grep a listing from a directory that has yesterday date in its file name then put it in the remote FTP server. How can I get it... (4 Replies)
Discussion started by: raouf@comcast.n
4 Replies

8. Shell Programming and Scripting

pipe output of grep to sed?

Is there a way I can do this: search for text and replace line containing matched text with a different line? For example: "I want to replace text" I want to search for replace and then change the line to I am perplexed. Hope that makes sense. Thanks in advance. (4 Replies)
Discussion started by: arsh
4 Replies

9. Shell Programming and Scripting

pipe grep command

Hi all, Can someone help me with the following problem. I am executing the following command: (search for occurences of 'error' in files that match cl-*.log expression) > grep -cw -i --max-count=1 'error' cl-*.log this command outputs: cl-apache.log:1 cl-apache_error.log:1... (3 Replies)
Discussion started by: epro66
3 Replies

10. UNIX for Dummies Questions & Answers

Filter results through pipe with grep

ls -ltr | grep string How can I use regular expressions to filter the results provided even more. I am using the above command as a reference. (1 Reply)
Discussion started by: ckandreou
1 Replies
Login or Register to Ask a Question