The UNIX and Linux Forums  

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
pipe grep command epro66 Shell Programming and Scripting 3 05-14-2009 07:17 PM
Is it better to grep and pipe to awk, or to seach with awk itself DeCoTwc Shell Programming and Scripting 4 10-07-2008 03:52 PM
Grep for NULL in a pipe delimited file sureshg_sampat Shell Programming and Scripting 5 11-21-2006 06:15 AM
Pipe Data From Grep Into A File katinicsdad Shell Programming and Scripting 4 09-08-2006 12:20 PM
Filter results through pipe with grep ckandreou UNIX for Dummies Questions & Answers 1 07-10-2006 03:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-31-2009
ShellBeginner ShellBeginner is offline
Registered User
  
 

Join Date: May 2009
Posts: 6
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 (permalink)  
Old 05-31-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 740
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 (permalink)  
Old 05-31-2009
ShellBeginner ShellBeginner is offline
Registered User
  
 

Join Date: May 2009
Posts: 6
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


Thank you anyway !!
  #4 (permalink)  
Old 05-31-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
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 (permalink)  
Old 05-31-2009
ShellBeginner ShellBeginner is offline
Registered User
  
 

Join Date: May 2009
Posts: 6
Quote:
Originally Posted by vidyadhar85 View Post
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 !
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 12:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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