在 space 之后将行的其余部分移到换行符
shift the rest of the line to a newline after a space
如果我有以下情况:
>AB ABABABA
>AC ACACACA
如何在 space 之后将所有内容移到换行符,即
>AB
ABABABABA
>AC
ACACACACA
我试过:
cat file | sed 's/ /\n/g'
cat file | tr ' ' '\n'
但是我得到了完全相同的输出。
** 更新 **
使用 less 和 nano 检查文件后,输出与使用 cat 不同。该文件包含一些未在 cat 中显示但在 less 中显示的终端转义字符。 (这是怎么发生的?)
这是一个很难发现的错误,实际上每个人都根据 cat 的输出发布了更正的答案。所以谢谢你的帮助。模组可以关闭这个吗?
您是否尝试将 space 之前的内容也移动到下一行?
如 >A BC
变为:
>A
ABC
然后可以像这样使用 sed:
$ sed 'h;s/^>\([^ ]*\) //;x;s/ .*/ /;G' file
>AB
ABABABABA
>AC
ACACACACA
细分:
h; # Copy pattern space to hold buffer
s/^>\([^ ]*\) //; # Convert >A BC to ABC
x; # eXchange hold buffer and pattern space
s/ .*/ /; # Remove everything after, but including the
# first space: >A BC -> >A
G # Append hold buffer to pattern space
看来您需要用换行符替换(任何一种)space
perl -pe 's/\s+/\n/' data.txt
这会在我的测试中产生所需的输出。 -p
设置输入循环(打开文件或使用 STDIN
)并将 $_
设置为当前行。它还在每次处理后打印 $_
。
如果有多个space,每个都被\n
替换,添加/g
修饰符。
如果还有更多工作要做,您还可以捕获模式并替换它们
perl -pe 's/\s+(.*)/\n/' data.txt
根据 中的观察并“更仔细地”看,似乎该行的第一个单词需要复制到下一行。然后将上面修改为
perl -pe 's/^>(\S+)\K\s+/\n';
\K
是 positive lookbehind, which asserts that the pattern preceeds the current match position but it discards all previous matches (so you don't have to capture and copy them). You can find it in perlre 的一种特殊形式。没有它,>(\S+)
将被消耗,因此必须将其复制回替换部分,如 />\n/
.
sed 用于在单行上进行简单替换,仅此而已。对于其他任何你应该使用 awk,例如:
$ awk '{print ORS substr(,2) }' file
>AB
ABABABABA
>AC
ACACACACA
仔细看,你似乎想重复下一行的第一个词:to transform this
>foo bar
>baz qux
进入这个
>foo
foobar
>baz
bazqux
如果那是真的,你可以
sed -r 's/^>([^ ]+) />\n/' file # or
perl -pe 's/^>(\S+) />\n/' file
如果我有以下情况:
>AB ABABABA
>AC ACACACA
如何在 space 之后将所有内容移到换行符,即
>AB
ABABABABA
>AC
ACACACACA
我试过:
cat file | sed 's/ /\n/g'
cat file | tr ' ' '\n'
但是我得到了完全相同的输出。
** 更新 **
使用 less 和 nano 检查文件后,输出与使用 cat 不同。该文件包含一些未在 cat 中显示但在 less 中显示的终端转义字符。 (这是怎么发生的?)
这是一个很难发现的错误,实际上每个人都根据 cat 的输出发布了更正的答案。所以谢谢你的帮助。模组可以关闭这个吗?
您是否尝试将 space 之前的内容也移动到下一行?
如 >A BC
变为:
>A
ABC
然后可以像这样使用 sed:
$ sed 'h;s/^>\([^ ]*\) //;x;s/ .*/ /;G' file
>AB
ABABABABA
>AC
ACACACACA
细分:
h; # Copy pattern space to hold buffer
s/^>\([^ ]*\) //; # Convert >A BC to ABC
x; # eXchange hold buffer and pattern space
s/ .*/ /; # Remove everything after, but including the
# first space: >A BC -> >A
G # Append hold buffer to pattern space
看来您需要用换行符替换(任何一种)space
perl -pe 's/\s+/\n/' data.txt
这会在我的测试中产生所需的输出。 -p
设置输入循环(打开文件或使用 STDIN
)并将 $_
设置为当前行。它还在每次处理后打印 $_
。
如果有多个space,每个都被\n
替换,添加/g
修饰符。
如果还有更多工作要做,您还可以捕获模式并替换它们
perl -pe 's/\s+(.*)/\n/' data.txt
根据
perl -pe 's/^>(\S+)\K\s+/\n';
\K
是 positive lookbehind, which asserts that the pattern preceeds the current match position but it discards all previous matches (so you don't have to capture and copy them). You can find it in perlre 的一种特殊形式。没有它,>(\S+)
将被消耗,因此必须将其复制回替换部分,如 />\n/
.
sed 用于在单行上进行简单替换,仅此而已。对于其他任何你应该使用 awk,例如:
$ awk '{print ORS substr(,2) }' file
>AB
ABABABABA
>AC
ACACACACA
仔细看,你似乎想重复下一行的第一个词:to transform this
>foo bar
>baz qux
进入这个
>foo
foobar
>baz
bazqux
如果那是真的,你可以
sed -r 's/^>([^ ]+) />\n/' file # or
perl -pe 's/^>(\S+) />\n/' file