0

I have a file like this called new.samples.dat

-4.5000000000E-01 8.0000000000E+00 -1.3000000000E-01
5.0000000000E-02 8.0000000000E+00 3.4000000000E-01
...

I have to search all this numbers of this file in another file called Remaining.Simulations.dat and copy them in another file. I did like this

for sample_index in $(seq 1 100)
do
  sample=$(awk 'NR=='$sample_index'' new.samples.dat)
  grep "$sample" Remaining.Simulations.dat >> Previous.Training.dat
done

It works almost fine but it does not copy all the $sample into Previous.Training.dat even if I am sure that these are in Remaining.Simulations.dat This errors appear

grep: invalid option -- '.'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

Do you have any idea how to solve it?Thank you

3
  • Now i added grep -- and it does not give that error anymore. But still it adds much more values than it should. In fact it finds almost 150 values instead of 100 Commented Jun 19, 2013 at 8:40
  • I am sure there are no doubles in Remaining.Simulations.dat Commented Jun 19, 2013 at 8:52
  • I think grep does not distinguish negative values Commented Jun 19, 2013 at 8:56

2 Answers 2

5

It's because you're trying to grep for something like -4.5 and grep is treating that as an option rather than a search string. If you use -- to indicate there are no more options, this should work okay:

pax> echo -4.5000000000E-01 | grep -4.5000000000E-01
grep: invalid option -- '.'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

pax> echo -4.5000000000E-01 | grep -- -4.5000000000E-01
-4.5000000000E-01

In addition, if you pass the string 7.2 to grep, it will match any line containing 7 followed by any character followed by 2 since:

  1. Regular expressions treat . as a special character; and
  2. Without start and end markers, 7.2 will also match 47.2, 7.25 and so on.
Sign up to request clarification or add additional context in comments.

2 Comments

@user18350, no that was just to demonstrate. All you need to do is put -- as the first argument to your grep so that it doesn't assume -4.5 is an option rather than an argument. But see specifically the next part of the post which talk about the problem of regexes. That will require you stepping back and telling us the problem you want solved rather than presuming a solution. It may well be that awk on its own will be better.
I have to search for all the values in new.samples.dat into the Remaining.Simulations.dat and then copy the whole line of the values that matches in Remaining.Simulations.dat into another file
1

With awk you can try something like:

awk '
NR==FNR {
    for (i=1;i<=NF;i++) {
        numbers[$i]++
    }
    next
}
{
    for (number in numbers)
        if (index ($0,number) > 0) {
            print $0

    }
}' new.samples.dat Remaining.Simulations.dat > anotherfile

1 Comment

thanks!this is what i was looking for. but i want that the first column of the first matches only with the first column of the second file and like this also for the other columns. Instead in this case matches also the second column with the first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.