1

I have a file test.csv with numbers:

>cat test.csv
32
23
45

I store the first value in a variable:

>e=$(cat test.csv | head -n 1)
>echo $e
32

However, I can't do arithmetic with this number, I have tried converting it to integer as explained here:

>echo "$((e + 1))"
")syntax error: invalid arithmetic operator (error token is "

EDIT: Issue might have to do with whitespaces, that I can't delete:

>c="32"
>echo $c | wc -c
3
>echo $c | wc -c
4
>echo $c | tr -d '[:space]' | wc -c
4
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Pedro
  • 355
  • 4
  • 18
  • 1
    Check your file for special characters: `cat -A file` or `cat -v file` – Cyrus May 27 '18 at 18:49
  • 1
    Looks like the file has DOS line endings. Notice the `'\r'` forcing the final `")` back to the front of the line in the error message? You can use `dos2unix filename` to fix that issue. A `hexdump -Cv filename` will show the `0xb` (13) character. – David C. Rankin May 27 '18 at 18:54
  • there is some strange `^M character` at the end of each line – Pedro May 27 '18 at 18:56
  • The `^M` is the *carriage-return*. (DOS or Mac - pre OSX line ending) (or just copy/paste the file from what you posted above which should strip the carriage returns as well, e.g. select your text above, `rm filename` then `vi filename`, `i` and `middle-mouse`, then `esc` and `:wq` – David C. Rankin May 27 '18 at 18:56
  • `dos2unix filename` solved it. Thanks! – Pedro May 27 '18 at 19:05

0 Answers0