Using sed, how do you print the first 'N' characters of a line?

Viewed 190625

Using sed what is an one liner to print the first n characters? I am doing the following:

grep -G 'defn -test.*' OctaneFullTest.clj  | sed ....
6 Answers

Don't use sed, use cut:

grep .... | cut -c 1-N

If you MUST use sed:

grep ... | sed -e 's/^\(.\{12\}\).*/\1/'

don't have to use grep either

an example:

sed -n '/searchwords/{s/^\(.\{12\}\).*/\1/g;p}' file

Strictly with sed:

grep ... | sed -e 's/^\(.\{N\}\).*$/\1/'

How about head ?

echo alonglineoftext | head -c 9
Related