I have the following problem:

I want to use a loop to plot data from different files (named file1.dat, file2.dat, ... in this example) in just one output-plot. I use this in a shell script to do so:

plot for [i=1:6] u 1:2 'file'.i.'.dat'

Now I additionally want to only plot a certain range of lines from each file. To do this I learned to use:

plot "<(sed -n '1,100p' file.dat)"  u 1:2

Anyhow, if I want to combine these two like:

plot for [i=1:6] "<(sed -n '1,100p' file'.i.'.dat)"  u 1:2

clearly the i within the expression giving the filename file'.i.'.dat to sed cannot be interpreted as the variable i. The error I get is:

sed: can't read file_.i..dat: No such file or directory

Do you have any suggestions how to circumvent this problem or other approaches to achieve my goal?

  • 4
    if you're getting an awk error, then you're using awk and not showing us. Could you share enough of the script for someone else to be able to reproduce the issue? – Jeff Schaller Sep 7 '17 at 17:36
  • Did you try with matching quotes for i to be interpreted as variable: plot for [i=1:6] "<(sed -n '1,100p' file".i.".dat)" u 1:2? – sebasth Sep 7 '17 at 18:13
  • You should be able to use every ::0::99 to select the first 100 data points. – meuh Sep 7 '17 at 19:59
  • 2
    On a side note: sed -n 1,100p is better written as sed 100q. – Satō Katsura Sep 7 '17 at 20:04

You have to change:

plot for [i=1:6] "<(sed -n '1,100p' file'.i.'.dat)"  u 1:2


plot for [i=1:6] "<(sed -n '1,100p' file".i.".dat)"  u 1:2

Note how the quotes changed. The strings go from one double quote to another. In your line there is only one string <(sed -n '1,100p' file'.i.'.dat) which is not what you want. In my line, there are two strings <(sed -n '1,100p' file and .dat) that are concatenated with the value of i.

Besides, no need to resort to sed to print the first 100 lines of a file, this is typically a job for head (head -n 100).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.