bash 如何在“使用 SED”模式之前插入字符串或换行符(不替换模式)在 MAC OS 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19589970/
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
How to insert string or newline before a pattern "Using SED" ( Without replacing the pattern) In MAC OS
提问by sam
I have a file with the following content:
我有一个包含以下内容的文件:
aaaabbaaabbaa
and i need an output like:
我需要一个输出,如:
aaaa
bbaaa
bbaa
I need a new line to be added before first occurrence of 'b'
. I need only SED command to use in bash
我需要在第一次出现之前添加一个新行'b'
。我只需要在 bash 中使用 SED 命令
I am using the following command. I know its now the perfect one..
我正在使用以下命令。我知道它现在是完美的..
Can anyone tell me a better command than this. Pl note only SED command i need to use in bash
谁能告诉我比这更好的命令。请注意我只需要在 bash 中使用的 SED 命令
sed -i.bak -e 's/bb/qbb/g' input.txt
sed -i.bak -e 's/qbb/\'$'\nbb/g' input.txt
回答by fedorqui 'SO stop harming'
With sed
:
与sed
:
$ echo "aaaabbaaabbaa" | sed -r 's/([b]+)/\n/g'
aaaa
bbaaa
bbaa
sed -r
allows to catch blocks with ()
and print them back with \1
. The block it catches it [b]+
, meaning "one or more b's"
, and prints it back preceded by a new line.
sed -r
允许使用 捕获块()
并将它们打印回来\1
。它捕获它的块[b]+
,意思是"one or more b's"
,并在一个新行之前将它打印回来。
As I see you are using sed -i
, it is also good to do:
正如我看到您正在使用的那样sed -i
,这样做也很好:
sed -i.bak -r 's/([b]+)/\n/g' input.txt
Also, easier (thanks Glenn Hymanman!)
此外,更容易(感谢格伦Hyman曼!)
$ echo "aaaabbaaabbaa" | sed 's/b\+/\n&/g'
aaaa
bbaaa
bbaa
It replaces all sequences of "b" and replaces that with a newline followed by that same sequence of "b" (&
represents whatever was matched on the left side of s///
).
它替换“b”的所有序列,并用换行符替换它,后跟“b”的相同序列(&
代表 左侧匹配的任何内容s///
)。
回答by anubhava
grep -oP
with lookahead regex will be easier:
grep -oP
使用前瞻正则表达式会更容易:
echo 'aaaabbaaabbaa' | grep -oP '.+?[^b](?=(b|$))'
aaaa
bbaaa
bbaa
回答by Digital Trauma
If your input string really does contain only a
and b
characters, then I think the problem degenerates to a simple replacement of all instances of ab
with a<newline>b
. If this is the case then you can omit sed
altogether and use the Shell Parameter Expansion feature in bash:
如果您的输入字符串确实只包含a
andb
字符,那么我认为问题会退化为简单替换ab
with的所有实例a<newline>b
。如果是这种情况,那么您可以sed
完全省略并使用bash 中的Shell 参数扩展功能:
At the terminal:
在终端:
$ str="aaaabbaaabbaa"
$ echo "${str//ab/a
> b}"
aaaa
bbaaa
bbaa
$
Or in a shell script:
或者在 shell 脚本中:
$ cat ab.sh
#!/bin/bash
echo "${1//ab/a
b}"
$ ./ab.sh "aaaabbaaabbaa"
aaaa
bbaaa
bbaa
$
This works for me on OSX 10.8.5.
这在 OSX 10.8.5 上对我有用。
This information is also available on the bash manpagehosted by apple.com. Search for "parameter/pattern" on that page.
此信息也可在apple.com 托管的bash 联机帮助页上找到。在该页面上搜索“参数/模式”。
回答by devnull
You could say:
你可以说:
$ echo aaaabbaaabbaa | sed 's/b\{1,\}/\'$'\n&/g'
aaaa
bbaaa
bbaa
or
或者
$ echo aaaabbaaabbaa | sed $'s/b\{1,\}/\\n&/g'
aaaa
bbaaa
bbaa
In order to make sed
interpret the regex as extended regular expressions, you could use the -E
option:
为了sed
将正则表达式解释为扩展正则表达式,您可以使用以下-E
选项:
$ echo aaaabbaaabbaa | sed -E 's/b+/\'$'\n&/g'
aaaa
bbaaa
bbaa
回答by Jotne
An ugly awk
version :)
一个丑陋的awk
版本:)
echo "aaaabbaaabbaa" | awk '{for (i=1;i<=NF;i++) {printf ($i=="b" && f!="b" ?"\n":"")"%s",$i; f=$i}} END {print ""}' FS=
aaaa
bbaaa
bbaa
A gnu awk
version
一个gnu awk
版本
echo "aaaabbaaabbaa" | awk '{=} NR>1 {echo "aaaabbaaabbaa" | awk 'gsub(/b+/,"\n&")'
aaaa
bbaaa
bbaa
=RS echo "aaaabbaaabbaa\nbbaabba" | sed 's/\([^b]\)b/\
b/g'
aaaa
bbaaa
bbaa
bbaa
bba
;} 1' RS="bb"
aaaa
bbaaa
bbaa
Another awk
. Replace any b
or group of b
with newline and itself &
另一个awk
。用换行符和它本身替换任何b
或一组b
&
sed -e :a -e '/ab\(.*\)\(.\)$/!b' -e G -e 's//ab/' -e ta file
回答by NeronLeVelu
sed 's/bb*/\n&/g' file
posix compliant and does not make a new line if line start with a b
posix 兼容,如果行以 ab 开头,则不换行
回答by potong
This might work for you:
这可能对你有用:
sed 's/bb*/'"\n"'&/g' file
This loops through the current line replacing any ab
combinations with a\nb
. It uses the hold space side-effect that a newline is always present when a new instance of sed is created.
这会遍历当前行,用 替换任何ab
组合a\nb
。它使用保留空间副作用,即在创建 sed 的新实例时始终存在换行符。
Of course:
当然:
$ echo -e "aaaabbaaabbaa\nbbaaaabbaaabbaa" | sed -e 's/\([^b]\)b/\nb/g'
aaaa
bbaaa
bbaa
bbaaaa
bbaaa
bbaa
or:
或者:
##代码##Is a lot easier but probably depends on a GNU version of sed or bash.
容易得多,但可能取决于 sed 或 bash 的 GNU 版本。
回答by jeff
sed -e 's/bb/\ nn/g' input.txt
sed -e 's/bb/\nn/g' input.txt
I got this to work. It is very similar to your original attempt. I am on an iMac, so I am pretty sure the same will work for you.
我得到了这个工作。这与您最初的尝试非常相似。我在 iMac 上,所以我很确定这对你有用。
回答by Hynek -Pichi- Vychodil
When you want avoid new line when b
occurs at beginning of line and all of this POSIX compliant.
当您想避免在行首b
出现时避免换行并且所有这些都符合 POSIX。