LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-28-2009, 03:40 PM   #1
anil3
LQ Newbie
 
Registered: Mar 2006
Location: Norway
Distribution: Fedora
Posts: 14

Rep: Reputation: 0
Strange error using grep in a while loop


Hello everybody.

I have a strange problem (aren't they all )

The challenge:
I have two files
#1 holds a simple list of 3000+ lines, each line beeing an order ID like for example 53267

This file is named ordreliste_2008.txt

#2 holds more than 33000 lines. Each line holds an information string like this
55072;;386;4;;175
the first 5 digits are the order ID.

This file is named ordrelinje.csv

I want to run a while loop iterating file #1 and grep the order ID in file #2.
The result will eventually be >> into a new file

I'm using this simple while loop

while read LINE
do
echo "this is LINE: $LINE" #simple check to see the value of the line
grep "$LINE" ordrelinje.csv
echo $? #simple check to see the result of last command
done < /home/noricom_bergen/ordreliste_2008.txt

My result is all negative - no output

I have tried to write '$LINE', $LINE - same result

If I do this:

while read LINE
do
echo "this is LINE: $LINE" #simple check to see the value of the line
grep 55072 ordrelinje.csv
echo $? #simple check to see the result of last command
done < /home/noricom_bergen/ordreliste_2008.txt

I get an output

The same happens if I put in
while read LINE
do
echo "this is LINE: $LINE" #simple check to see the value of the line
test=52072
grep $test
ordrelinje.csv
echo $? #simple check to see the result of last command
done < /home/noricom_bergen/ordreliste_2008.txt

If I however put in test=$LINE the output is nothing


It is only when I use the variable $LINE together with grep that I get this peculiar error.
My conclution is that the grep itself is working ok - I've proved that, but there is a catch with the variable $LINE.

Anyone out there who can help me out?
 
Old 03-28-2009, 04:10 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

Just a guess, but your files might have DOS linebreaks. Try running dos2unix on ordreliste_2008.txt first.

Dave
 
Old 03-28-2009, 07:01 PM   #3
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Rep: Reputation: 22
From what I understand, $LINE will contain the full line, i.e., 55072;;386;4;;175. If you really only want the ID, you have to split the line, one simple way is to use command substitution:

Code:
LINE="55072;;386;4;;175"
grep ${LINE%%;*} ordrelinje.csv
${LINE%%;*} will remove everything after (and including) the first semicolon encountered in the string.


EDIT: Sorry, I have just seen that I misunderstood what you were trying to do.

Last edited by synss; 03-28-2009 at 07:03 PM.
 
Old 03-29-2009, 04:26 PM   #4
moopet
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
I would suggest

Code:
echo "this is LINE: '$LINE'" #simple check to see the value of the line
to make sure there are no newlines or whitespace around $LINE and if there is, go from there.
 
Old 03-30-2009, 03:09 AM   #5
vamped
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Rep: Reputation: 0
I agree that there probably are invisible characters in $LINE.

Your script appears to work fine when file #1 contains only numbers.
You could try cleansing your file with this command:
(after you make a backup just in case you need it)

Code:
tr -cs '[:digit:]' '\n' < ordreliste_2008.txt > tempfile; mv tempfile ordreliste_2008.txt
The command searches for anything that is not a number and replaces it with a single new-line.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep in a for loop. rose_bud4201 Programming 13 03-18-2009 04:02 AM
Grep result in a loop and linefeed issue romainp Programming 2 06-13-2008 10:11 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 10:27 PM
problem with 'rpm -qa | grep' in while loop ionic_slim Programming 9 11-11-2007 08:09 AM
Strange behaviour in bash - while loop Guttorm Programming 4 06-08-2007 02:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration