使用sed,如何打印一行的第一个“N”字符?

时间:2021-05-08 08:57:00

With sed, what is a one liner to print the first n characters. I am doing the following.

使用sed,打印前n个字符的单个衬垫是什么。我正在做以下事情。

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

5 个解决方案

#1


Don't use sed, use cut.

不要使用sed,使用切割。

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

If you MUST use sed:

如果你必须使用sed:

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

#2


colrm x

For example, if you need the first 100 characters:

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

cat file |colrm 101 

It's been around for years and is in most linux's and bsd's (freebsd for sure), usually by default. I can't remember ever having to type apt-get install colrm.

它已存在多年,并且通常默认情况下是大多数linux和bsd(肯定是freebsd)。我不记得曾经输入过apt-get install colrm。

#3


don't have to use grep either

也不必使用grep

an example:

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

#4


Strictly with sed:

严格用sed:

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

#5


To print the N first characters you can remove the N+1 characters up to the end of line:

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

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

#1


Don't use sed, use cut.

不要使用sed,使用切割。

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

If you MUST use sed:

如果你必须使用sed:

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

#2


colrm x

For example, if you need the first 100 characters:

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

cat file |colrm 101 

It's been around for years and is in most linux's and bsd's (freebsd for sure), usually by default. I can't remember ever having to type apt-get install colrm.

它已存在多年,并且通常默认情况下是大多数linux和bsd(肯定是freebsd)。我不记得曾经输入过apt-get install colrm。

#3


don't have to use grep either

也不必使用grep

an example:

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

#4


Strictly with sed:

严格用sed:

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

#5


To print the N first characters you can remove the N+1 characters up to the end of line:

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

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