Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read characters from a text file using bash

Does anyone know how I can read the first two characters from a file, using a bash script. The file in question is actually an I/O driver, it has no new line characters in it, and is in effect infinitely long.

like image 527
Simon Hodgson Avatar asked Dec 04 '22 15:12

Simon Hodgson


1 Answers

The read builtin supports the -n parameter:

$ echo "Two chars" | while read -n 2 i; do echo $i; done
Tw
o
ch
ar
s

$ cat /proc/your_driver | (read -n 2 i; echo $i;)
like image 98
Vinko Vrsalovic Avatar answered Dec 11 '22 16:12

Vinko Vrsalovic