我需要一个命令来在段落的末尾添加句点(句号)。我尝试了以下命令:
sed '/ +$ / s/$/ ./' $FILENAME
但它不起作用!!
答案 0 :(得分:2)
awk -v RS="" -v ORS=".\n\n" 1 file
这会将输入记录分隔符重新定义为空,以便awk将空白行分隔的段落作为单个记录读取。它将输出记录分隔符设置为点和2个换行符。实际的awk程序1
简单打印每条记录。
一个副作用是任何连续的空行都会折叠成一个空白行。
好的,谢谢
awk -v RS="" -v ORS="\n\n" '{sub(/\.?$/,".")} 1'
在行动中:(通过cat -n
管道只是为了指出换行符)
echo -e "a.\n\nb\nc\n\n\nd" |
awk -v RS="" -v ORS="\n\n" '{sub(/\.?$/,".")} 1' |
cat -n
1 a.
2
3 b
4 c.
5
6 d.
7
由于ORS,最后会有一个额外的换行符。
而且,作为奖励,这里有一些保留段落间距的Perl:
echo -e "a.\n\nb\nc\n\n\nd" | perl -0777 -pe 's/\.?(\n(\n+|$))/.$1/g' | cat -n
1 a.
2
3 b
4 c.
5
6
7 d.
答案 1 :(得分:1)
不太好,但似乎有用......
$ cat input
This is a paragraph with some text. Some random text that is not really important.
This is another paragraph with some text.
However this sentence is still in the same paragraph
$ tr '\n' '@' < input | sed 's/\([^.]\)@@/\1.@@/g' | tr '@' '\n'
This is a paragraph with some text. Some random text that is not really important.
This is another paragraph with some text.
However this sentence is still in the same paragraph.
答案 2 :(得分:1)
使用sed。
sed ':loop;$!{N;b loop};s/[^\.]$/&./;s/\([^\.]\)\(\n[ \t]*\n\)/\1.\2/g' file
解释
:loop;$!{N;b loop}
将保存换行符分隔的模式空间中的所有行。
s/[^.]$/&./
会添加。如果最后一段没有点到底。
s/\([^\.]\)\(\n[ \t]*\n\)/\1.\2/g
将在\ n \ n之前添加点,这将标识为新段落。
答案 3 :(得分:1)
在保留空间中累计'paragraph'。只要保持积累 输入行包含任何非空格字符。
当您获得空白/空行时,假设您有一个累积的段落。用保持空间交换当前(空白)线。替换模式空间中的最后一个非空格字符(现在是您正在积累的“段落”),后跟一个点,除非该字符是点。打印结果。
我认为这样做:
$ cat test
this is a test line. one-line para
this is a test line. one-line para. with period.
this is a
two line para-
graph with dot.
this is a
two-line paragraph
with no dot
also works on last
line of file
$ sed -n \
-e '/^[[:space:]]*$/{x;s/\([^.[:space:]][[:space:]]*\)$/\1./;p;n;}' \
-e '/^[[:space:]]*[^[:space:]]/H' \
test
this is a test line. one-line para.
this is a test line. one-line para. with period.
this is a
two line para-
graph with dot.
this is a
two-line paragraph
with no dot.
答案 4 :(得分:0)
这应该有效:
sed "s/[[:alpha:]]\+[^\.]$/\./" $FILENAME
答案 5 :(得分:0)
纯sed解决方案,使用保留空间保存段落中的所有行,并在打印前附加句点:
sed -ne '
## Append current line to "hold space".
H
## When found an empty line, get content of "hold space", remove leading
## newline added by "H" command, append a period at the end and print.
## Also, clean "hold space" to save following paragraph.
/^$/ { g; s/^\n//; s/\(.*\)\(\n\)/\1.\2/; p; s/^.*$//; h; b }
## Last line is a bit special, it has no following blank line but it is also
## an end of paragraph. It is similar to previous case but simpler.
$ { x; s/^\n//; s/$/./; p }
' infile
假设infile
包含内容:
one
two
three
four
five
six
它产生:
one
two.
three.
four
five
six.