LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: while true to "read" a file (https://www.linuxquestions.org/questions/programming-9/bash-while-true-to-read-a-file-832684/)

SilversleevesX 09-16-2010 07:25 PM

BASH: while true to "read" a file
 
Jog my memory somebody -- Google evidently has no idea what I mean.

I had a script once that had in it a while true; do and read lines or data from a file similarly to a while read; do. I have a script I'm working on now where I'm trying to "prove the negative" that certain substrings culled from one file do not appear in another file.

I'll try to explain what I mean with an example.

An item in one list might be
Code:

harvey1998nicks211.jpg
But the other list may only have
Code:

harvey1996nicks:Six stores in north London
harvey1997nicks:Three stores in Bristol
harvey1999nicks:Eight stores in the lower Midlands

The problem is, other items in the first list do match items in the second. The script I'm drawing this from has already pulled those out and written them to one (or several) lists that I've been able to use with other programs (Exiv2 specifically).

The furthest I've bothered to go in the direction of "proving the negative" is a slow "if/then/fi" loop that looks like this:
Code:

linecheck=$[linecheck+1]
if [[ "$linecheck" == "$axe" ]] && [[ "$matchX" == "" ]]; then echo -e $file>>nomatches.txt; fi

linecheck is counting the lines of the second file -- the one with the designations or "labels" (the "so many stores in ..." from my example) -- as they are checked against the file names in the first (the "harvey199*nicks.jpg" etc of my example. axe is the number of lines in that first list (culled from a cat |wc -l at the beginning of the script). matchX is the variable the "prove the positive" script used for matches, one list to the other, which then got written line by line to a text file.

I have the strong feeling that either a while true do or a better-written if/then/fi loop is what I'm looking for. If there are other methods, I'll certainly entertain any suggestions in that direction.

BZT

SilversleevesX 09-16-2010 09:13 PM

It was a case for "case"
 
The code that saved the day:
Code:

case "$matchX" in
  "") echo -e "$file">>nomatches.txt
  ;;
  *) ;;
esac

I forgot to mention, that slow if/then/fi in the other script was also giving me no more than the first line item that met its conditions. This case/esac loop does turn out a nice long list, with a lot of duplicated lines, but I already knew how to deal with that *sort* of thing (pvmi):

Code:

sort -t"/" -k6 -u nomatches.txt -o notamatch.txt
rm nomatches.txt
mv -v notamatch.txt nomatches.txt

Sometimes a forum serves well just to help one sort things out in print & in "public." It gets one thinking faster and harder, at any rate.

BZT


All times are GMT -5. The time now is 04:40 PM.