0

I am looping through the lines in a text file. And performing grep on each lines through directories. like below

while IFS="" read -r p || [ -n "$p" ]
do
 echo "This is the field: $p"
  grep -ilr $p * >> Result.txt
done < fields.txt

But the above writes the results for the last line in the file. And not for the other lines.

If i manually execute the command with the other lines, it works (which mean the match were found). Anything that i am missing here? Thanks

The fields.txt looks like this

annual_of_measure__c
attached_lobs__c
apple 
OK999
  • 1,353
  • 2
  • 19
  • 39

1 Answers1

1

When the file fields.txt

  1. has DOS/Windows lineending convention consisting of two character (Carriage-Return AND Linefeed) and
  2. that file is processed by Unix-Tools expecting Unix lineendings consisting of only one character (Linefeed)

then the line read by the read command and stored in the variable $p is in the first line annual_of_measure__c\r (note the additional \r for the Carriage-Return). Then grep will not find a match.

From your description in the question and the confirmation in the comments, it seems that the last line in fields.txt has no lineending at all, so the variable $p is the ordinary string apple and grep can find a match on the last line of the file.

There are tools for converting lineendings, e.g. see this answer or even more options in this answer.

Lars Fischer
  • 9,135
  • 3
  • 26
  • 35
  • The above explanation is spot on. I am using a pc and the file have the DOS lineending convention. The same file is process in a bash shell (git bash on windows). i used the dos2unix executable to convert the file. – OK999 Oct 02 '18 at 15:41