bash 使用 sed 插入换行符 (\n)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46082397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert newline (\n) using sed
提问by Allan
I am trying to scrub some lists into a properly formatted CSV file for database import.
我正在尝试将一些列表整理到格式正确的 CSV 文件中以进行数据库导入。
My starting file, looks something like this with what is supposed to be each "line" spanning multiple lines like below
我的起始文件看起来像这样,每个“行”应该跨越多行,如下所示
Mr. John Doe
Exclusively Stuff, 186
Caravelle Drive, Ponte Vedra
33487.
I created a sed
script that cleans up the the file (there's lots of "dirty" formatting like double spaces and spaces before/after commas). The problem is the Zip with the period.I would like to change that period for a new line, but I cannot get it to work.
我创建了一个sed
清理文件的脚本(有很多“脏”格式,比如双空格和逗号前/后的空格)。 问题是带有句号的 Zip。我想为一条新线路更改该时间段,但我无法使其正常工作。
The command that I use is:
我使用的命令是:
sed -E -f scrub.sed test.txt
and the scrub.sed
script is as follows:
和scrub.sed
脚本如下:
:a
N
s|[[:space:]][[:space:]]| |g
s|,[[:space:]]|,|g
s|[[:space:]],|,|g
s|\n| |g
s|[[:space:]]([0-9]{5})\.|,FL,\n |g
$!ba
What I get is
我得到的是
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487n
If figured that the Zip+.(period) would be a great "delimiter" to use the substitution on and while I can find it, I can't seem to tell it to put a newline there.
如果认为 Zip+.(period) 将是一个很好的“分隔符”来使用替换,而我可以找到它,我似乎无法告诉它在那里放一个换行符。
Most of the things I found online are about replacing the newline with something else (usually deleting them), but not much on replacing with a newline. I did find this, but it didn't work: How to insert newline character after comma in `),(` with sed?
我在网上找到的大部分内容都是关于用其他东西替换换行符(通常是删除它们),但关于用换行符替换的内容并不多。我确实找到了这个,但它没有用: How to insert newline character after逗号 in `),(` with sed?
Is there something I am missing?
有什么我想念的吗?
Update:
更新:
I edited my scrub.sed file putting the literal new line as instucted. It still doesn't work
我编辑了我的scrub.sed 文件,按照指示添加了文字新行。它仍然不起作用
:a
N
s|[[:space:]][[:space:]]| |g
s|,[[:space:]]|,|g
s|[[:space:]],|,|g
s|\n| |g
s|[[:space:]]([0-9]{5})\.|,FL,\
|g
$!ba
What I get is (everything on one line):
我得到的是(一行的所有内容):
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487 Mrs. Jane Smith,Props and Stuff,123 Main Drive,Hymansonville,FL,336907
My expected output should be:
我的预期输出应该是:
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487
Mrs. Jane Smith,Props and Stuff,123 Main Drive,Hymansonville,FL,336907
回答by dawg
The sed
on BSD does not support the \n
representation of a new line (turning it into a literal n
):
在sed
上BSD不支持\n
新的线(将其变成文字的表示n
):
$ echo "123." | sed -E 's/([[:digit:]]*)\./\n next line/'
123n next line
GNU sed
does support the \n
representation:
GNUsed
确实支持\n
表示:
$ echo "123." | gsed -E 's/([[:digit:]]*)\./\nnext line/'
123
next line
Alternatives are:
替代方案是:
Use a single character delimiter that you then use tr
translate into a new line:
使用单个字符分隔符,然后将其tr
转换为新行:
$ echo "123." | sed -E 's/([[:digit:]]*)\./|next line/' | tr '|' '\n'
123
next line
Or use an escaped literal new line in your sed script:
或者在您的 sed 脚本中使用转义文字新行:
$ echo "123." | sed -E 's/([[:digit:]]*)\./\
next line/'
123
next line
Or use awk
:
或使用awk
:
$ echo "123." | awk '/^[[:digit:]]+\./{sub(/\./,"\nnext line")} 1'
123
next line
Or use GNU sed which supports \n
或者使用支持的 GNU sed \n
回答by Ed Morton
The portable way to get a newline in sed is a backslash followed by a literal newline:
在 sed 中获取换行符的可移植方式是反斜杠后跟文字换行符:
$ echo 'foo' | sed 's/foo/foo\
bar/'
foo
bar
I guarantee there's a far simpler solution to your whole problem by using awk rather than sed though.
我保证通过使用 awk 而不是 sed 可以为您的整个问题提供一个更简单的解决方案。
回答by ragerdl
The following works on Oracle Linux, x8664:
以下适用于 Oracle Linux, x8664:
$ echo 'foobar' | sed 's/foo/foo\n/'
foo
bar
If you need it to match more than once per line, you'll need to place a g
at the end, as in:
如果您需要每行匹配多次,则需要g
在末尾放置 a ,如下所示:
$ echo 'foobarfoobaz' | sed 's/foo/foo\n/g'
foo
barfoo
baz