Is there a command like cat
in linux which can return a specified quantity of characters from a file?
e.g., I have a text file like:
Hello world
this is the second line
this is the third line
And I want something that would return the first 5 characters, which would be "hello".
thanks
You can use dd to extract arbitrary chunks of bytes.
For example,
would copy bytes 1235 to 1239 from its input to its output, and discard the rest.
To just get the first five bytes from standard input, do:
Note that, if you want to specify the input file name, dd has old-fashioned argument parsing, so you would do:
Note also that dd verbosely announces what it did, so to toss that away, do:
or
head:
Name
head - output the first part of files
Synopsis
head [OPTION]... [FILE]...
Description
Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=[-]N print the first N bytes of each file; with the leading '-', print all but the last N bytes of each file
you could also grep the line out and then cut it like for instance:
grep 'text' filename | cut -c 1-5