so I have a file step1 containing data like

field [T]:0.000
heatstart [s]: 1.50E+1

and a file called var4 containing only the entry 1.50E+1. I used mapfile to get this entry as a variable

mapfile -t var<var4

which grep can use to search in the data to give me the line number

grep -n "${var[0]}" step1

Using this gives me only the entry in line 2 of step1, not the entry in line 5. If I copy the entry from line 5 into another line at the beginning, I don't get it with grep either, but if I copy it somewhere in the middle of a line grep gets it. But if I paste the file step1 to itself, I don't get the other results.

I also tried getting the variable for 1.50E+1 with sed and cut as

sed -n '2p' < step1>var3
var4="$(cut -c15- var3)"

but this results into the same problem. There is no space in front of the number in the variable, because even if I cut so much of that the 1 isn't witin the variable anymore, I don't get all results. Does someone know why this happens or how to get around this?

1 Answer 1


I take it you're going to have a lot of stepN and varN files?

I would use for grep's -f option and the -n as you have.

grep -n -f var4 step4

That outputs:

mdiehn@frank ~ $ grep !*
grep -n -f var4 step4
1:tstart [s]: 1.50E+1

If you don't want tstart, either eliminate it with 'grep -v' or we could get into some more interesting awk scripting.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .