0

So this behavior is really weird. I have always accessed a file line by line, get that variable in $i and then grep this value from a file to output to a file, but now it looks that everything up to the variable is being overwritten with the characters after the variables. Look:

for i in $(cat tissues); do echo "grep -w $i";done
grep -w AdiposeTissue
grep -w AdrenalGland
grep -w Bladder

Then when I do (some examples) :

for i in $(cat tissues); do echo "grep -w $i AnyInterestingFile.txt";done
 AnyInterestingFile.txt
 AnyInterestingFile.txt
 AnyInterestingFile.txt

for i in $(cat tissues); do echo "grep -w $i AnyInterestingFile.txt param1 param2";done
 AnyInterestingFile.txt param1 param2
 AnyInterestingFile.txt param1 param2
 AnyInterestingFile.txt param1 param2

for i in $(cat tissues); do echo "grep -w $i Anyfile";done
 AnyfileAdiposeTissue
 AnyfileAdrenalGland
 AnyfileBladder
  • 4
    The `tissues` file has CRLF newlines. Use `dos2unix` to fix it. – Barmar Feb 20 '20 at 16:53
  • 4
    BTW, quite unrelated to this question, `for i in $(anything)` is generally buggy (expands wildcards, splits on whitespace not just newlines, etc) -- see [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29), and [BashFAQ #1](http://mywiki.wooledge.org/BashFAQ/001) for the preferable practice. Also, [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor). – Charles Duffy Feb 20 '20 at 16:55

0 Answers0