【问题标题】:How can I move a column with variable length in between one vertical bar "|" and "["?如何在一个竖线“|”之间移动长度可变的列和 ”[”?
【发布时间】:2019-07-24 07:26:04
【问题描述】:

我的文件有 4000k 行。我需要重新格式化它。所以,我正在尝试记事本++(或awk)。每一行的结构都是

acc|GENBANK|ABJ91977.1|GENBANK|DQ876324|pol 蛋白 Tabulator[人类免疫缺陷病毒 1]TabulatorTLWQRPFVTIKVGGQLKEALLDTGADDTVLEEIELPGRWKPKMIGGIGGFIKVRQYDQIXVEICGHKAIGTVLVGPTPVNVIGRNLMTQIGCTLN

第4个竖线|和第一个[之间的字符是可变长度的。只有我在寻找提示或在哪里集中精力自己做。我尝试用 awk 打印,但是如何有一个部分长度可变,我得到了不同的结果。我也不能按列选择。

我想获取这个结构的文件

acc|GENBANK|ABJ91977.1|GENBANK|DQ876324,acc|GENBANK|ABJ91977.1|GENBANK|DQ876324,pol 蛋白

和其他具有这种结构的文件

acc|GENBANK|ABJ91977.1|GENBANK|DQ876324制表符TLWQRPFVTIKVGGQLKEALLDTGADDTVLEEIELPGRWKPKMIGGIGGFIKVRQYDQIXVEICGHKAIGTVLVGPTPVNVIGRNLMTQIGCTLN

TAB 是粗体字 - Tabulator

【问题讨论】:

  • “制表符”是什么意思?是TAB字符吗?您真的要重复第 5 列吗?
  • 制表符是制表符。是的,需要复制输出。下一个管道使用此输出文件。 -_-

标签: regex awk notepad++ bioinformatics


【解决方案1】:

这是第一个文件的一种方法。

  • Ctrl+H
  • 查找内容:(^[^|]+(?:\|[^|]+){4})\|(.+?)\h+\[.+$
  • 替换为:$1,$1,$2
  • 检查环绕
  • 检查正则表达式
  • 取消选中. matches newline
  • 全部替换

说明:

(               # group 1
  ^             # beginning of line
  [^|]+         # 1 or more non pipe
  (?:           # start non capture group
    \|          # a pipe
    [^|]+       # 1 or more non pipe
  ){4}          # end group, must appear 4 times
)               # end group 1
\|              # a pipe
(.+?)           # group 2, 1 or more any character but newline, not greedy
\h+             # 1 or more horizontal spaces (space or tabulation)
\[              # 1 openning square bracket
.+              # 1 or more any character but newline
$               # end of line

替换:

$1              # content of group 1 
,               # a comma
$1              # content of group 1 
,               # a comma
$2              # content of group 2

给定示例的结果:

acc|GENBANK|ABJ91977.1|GENBANK|DQ876324,acc|GENBANK|ABJ91977.1|GENBANK|DQ876324,pol protein

屏幕截图:


对于第二个文件:

  • Ctrl+H
  • 查找内容:(^[^|]+(?:\|[^|]+){4})\|.+?\h+\[.+?\](.+)$
  • 替换为:$1$2
  • 检查环绕
  • 检查正则表达式
  • 取消选中. matches newline
  • 全部替换

说明:

(               # group 1
  ^             # beginning of line
  [^|]+         # 1 or more non pipe
  (?:           # start non capture group
    \|          # a pipe
    [^|]+       # 1 or more non pipe
  ){4}          # end group, must appear 4 times
)               # end group 1
\|              # a pipe
.+?             # 1 or more any character but newline, not greedy
\h+             # 1 or more horizontal spaces (space or tabulation)
\[              # 1 openning square bracket
.+?             # 1 or more any character but newline, not greedy
\]              # a closing square bracket
(.+)            # group 2, 1 or more any character but newline
$               # end of line

屏幕截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2015-10-16
    相关资源
    最近更新 更多