The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read a file line by line VENC22 UNIX for Dummies Questions & Answers 4 3 Weeks Ago 08:09 AM
Read logline line by line with awk/sed dejavu88 Shell Programming and Scripting 7 05-26-2008 05:44 PM
read the file line by line kittusri9 Shell Programming and Scripting 3 04-24-2008 05:26 AM
Read line by line from a variable. sathishkmrv Shell Programming and Scripting 9 09-13-2007 07:25 AM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 06:47 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-18-2006
Registered User
 

Join Date: Jan 2006
Posts: 3
Unhappy HELP! problems with using 'cat' in 'while read line'

Hi,
I'm having some trouble reading a file that was 'cat' through a while loop. Can anyone suggest alternatives? what i do is

cat filename|grep [23]*.stuff
while read line
do
echo $line
... and other commands
done

The cat,grep line seems to work correctly, but the script hangs when i add in the while loop. echo line deosn't seem to work either.

I've been fiddling around, and sometimes I get a messagefor 'a broken pipe' and a syntax error, unexpected token near done? Help please! Been trying for hours. Thanks!

Last edited by chugger06; 01-18-2006 at 09:59 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 01-18-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
cat filename|grep [23]*.stuff
while read line
do
echo $line
... and other commands
done


you are just missing "|" at the end of grep, hence no input is going to while loop...

cat filename|grep [23]*.stuff |

moreover, grep command also don't look proper..
can you tell me what you are trying to grep actually if you are not getting the expected output
Reply With Quote
  #3  
Old 01-18-2006
Registered User
 

Join Date: Jan 2006
Posts: 3
ANDing 2 files

Hi,
Thanks for replying. That wa at least part of my problem as so obvious!!
filename contains a list of file names with different extensions. i just want files with a particular extension, 'stuff' in the example to do processing on. itmust have been looping infinitely.

thanks heaps. do you have suggestions for comparing the contents of two very similar files, when i need to be able to output a true/false value against a particular line? it's like an AND operation, with 2 different sets, not the same size.

is bash enough, or would perl be better? Thans again for helping.
Reply With Quote
  #4  
Old 01-18-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Yes you can use perl, that would be better than bash..

But if there is any extra line in middle on any one file, then your script will show that all lines after that extra line are not matching though the data might match, because you are comparing data against exact line numbers. You need to put extra efforts here to match this kind of stuff like what diff unix command does, diff unix command won't match based on the line numbers...

Why can't you use unix diff command ?? here is an example


$ more datafile
abcdefghijklmnopqrstuvwxyz.
123456789009876543211234567
This line to be removed.
zyxwvutsrqponmlkjihgfedcba.

$ more datafile1
abcdefghijklmnopqrstuvwxyz.
1234567890098765432112345672
hello how are you
This line to be removed.
zyxwvutsrqponmlkjihgfedcba.4

$ $ diff datafile datafile1
2c2,3
< 123456789009876543211234567
---
> 1234567890098765432112345672
> hello how are you
4c5
< zyxwvutsrqponmlkjihgfedcba.
---
> zyxwvutsrqponmlkjihgfedcba.4



in the above diff, you can see (highlighted in bold)

<number>c<number>,<number>.......

i.e first number before character "c" is the line number from first file and numbers after "c" are numbers from 2nd file in diff command.

2c2,3 mean line number 2 in datafile is different from line numbers 2 and 3 in datafile1. Here you can see that line 3 in datafile1 is an extra line that is completly missing in datafile, so diff ignores this line and starts comparing datafile line 3 against datafile1 line 4.... not line 3 in datafile against line 3 in datafile 4.

You can actually egrep "^[0-9]*c[0-9]*[,0-9]*" or egrep -v "<|>|-" from the diff output, to get the lines which differ and the make use of it... based on the line numbers you can set the true or false..

not sure there is a simple way.. may be i can take a look if i find time
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0