0

I am trying to read out a line from a file with sed.

a=0
atoms=$(sed -n '6p' < "POSCAR_""$a")
echo $atoms

This gives the following error: : no such file or directory

I know the file exists because when I try the same command with the manually written file name it gives the correct output.

I think the problem lies in the way I input the file with a variable and a prefix, but I cannot find any solution to this problem.

Wanted output: K Nb O Which is the sixth line in the file POSCAR_0.

  • Your code works fine for me. – John1024 Nov 29 '17 at 20:12
  • As an aside, unless you want word splitting and pathname expansion, variables should be double-quoted like `echo "$atoms"`. – John1024 Nov 29 '17 at 20:13
  • 1
    Just get rid of the `<` thing. And you seem to have quotes you don't need. So: `atoms=$( sed -n '6p' "POSCAR_$a" )` – Jack Nov 29 '17 at 20:21
  • 2
    That error message makes me think that there are DOS/Windows-style line endings involved, and that `$a` is actually getting set to `0`. See "[Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings)" for more info. – Gordon Davisson Nov 29 '17 at 20:31
  • As per @GordonDavisson's suggestion, I tried `a=$'0\r'` and that gives me the same error message that the OP sees. – John1024 Nov 29 '17 at 20:44
  • can you give the output of `cat -vte POSCAR_0 | head -6` ? – Nahuel Fouilleul Nov 29 '17 at 20:55
  • @NahuelFouilleul I don't think the problem is in the POSAR_0 file (although there might be a problem there too), but in the script that's trying to access it. But yeah, `cat -vte` will give a much better idea what's really in the file by making normally-invisible characters visible. – Gordon Davisson Nov 29 '17 at 22:45

0 Answers0