使用 sed 插入换行符 ( ) [英] Insert newline ( ) using sed
问题描述
我正在尝试将一些列表整理到格式正确的 CSV 文件中以进行数据库导入.
I am trying to scrub some lists into a properly formatted CSV file for database import.
我的起始文件看起来像这样,每个行"应该是什么跨越多行,如下所示
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.
我创建了一个 sed
脚本来清理文件(有很多脏"格式,比如双空格和逗号前后的空格).问题是带有句号的 Zip.我想将该句号更改为一个新行,但我无法让它工作.
I created a sed
script that cleans up 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 -E -f scrub.sed test.txt
和scrub.sed
脚本如下:
:a
N
s|[[:space:]][[:space:]]| |g
s|,[[:space:]]|,|g
s|[[:space:]],|,|g
s|
| |g
s|[[:space:]]([0-9]{5}).|,FL,1
|g
$!ba
我得到的是
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487n
如果认为 Zip+.(句点) 将是一个很好的分隔符"使用替换,虽然我能找到它,但我似乎无法告诉它在那里放一个换行符.
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.
我在网上找到的大部分内容都是关于用其他东西替换换行符(通常是删除它们),但关于用换行符替换的内容并不多.我确实找到了这个,但它没有用:如何在逗号后插入换行符`),(` with sed?
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?
有什么我遗漏的吗?
更新:
我编辑了我的scrub.sed 文件,按照指示添加了文字新行.还是不行
I edited my scrub.sed file putting the literal new line as instucted. It still doesn't work
:a
N
s|[[:space:]][[:space:]]| |g
s|,[[:space:]]|,|g
s|[[:space:]],|,|g
s|
| |g
s|[[:space:]]([0-9]{5}).|,FL,1
|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,Jacksonville,FL,336907
我的预期输出应该是:
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487
Mrs. Jane Smith,Props and Stuff,123 Main Drive,Jacksonville,FL,336907
推荐答案
BSD 上的 sed
不支持换行的
表示(把它变成文字 n
):
The sed
on BSD does not support the
representation of a new line (turning it into a literal n
):
$ echo "123." | sed -E 's/([[:digit:]]*)./1
next line/'
123n next line
GNU sed
确实支持
表示:
GNU sed
does support the
representation:
$ echo "123." | gsed -E 's/([[:digit:]]*)./1
next line/'
123
next line
替代方案是:
使用单个字符分隔符,然后使用 tr
转换为新行:
Use a single character delimiter that you then use tr
translate into a new line:
$ echo "123." | sed -E 's/([[:digit:]]*)./1|next line/' | tr '|' '
'
123
next line
或者在您的 sed 脚本中使用转义的文字新行:
Or use an escaped literal new line in your sed script:
$ echo "123." | sed -E 's/([[:digit:]]*)./1
next line/'
123
next line
或者定义一个新行:
POSIX:
nl='
'
BASH/zsh/其他支持 ANSI C 引用一个>:
BASH / zsh / others that support ANSI C quoting:
nl=$'
'
然后使用带有适当引用和转义的 sed
插入文字
:
And then use sed
with appropriate quoting and escapes to insert the literal
:
echo "123." | sed 's/./'"\${nl}"'next line/'
123
next line
或者使用awk
:
$ echo "123." | awk '/^[[:digit:]]+./{sub(/./,"
next line")} 1'
123
next line
或者使用支持
Or use GNU sed which supports
这篇关于使用 sed 插入换行符 ( )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!