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

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 ....
193557 次浏览

不要使用 sed,使用 cut:

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

如果你必须使用 sed:

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

严格执行:

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

也不用用 grep

例如:

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

例如,如果需要前100个字符:

cat file |colrm 101

它已经存在了很多年,并且在大多数 linux 和 bsd (当然是 freebsd)中,通常是默认的。我不记得我有打过 apt-get install colrm

要打印 N 个第一个字符,可以删除 N + 1个字符,直到行尾:

$ sed 's/.//5g' <<< "defn-test"
defn

呢?

echo alonglineoftext | head -c 9