![将上一行的部分内容复制到下面](https://linux22.com/image/97152/%E5%B0%86%E4%B8%8A%E4%B8%80%E8%A1%8C%E7%9A%84%E9%83%A8%E5%88%86%E5%86%85%E5%AE%B9%E5%A4%8D%E5%88%B6%E5%88%B0%E4%B8%8B%E9%9D%A2.png)
我有一个具有以下结构的文件:
GO:0000001 mitochondrion inheritance
GO:0000002 mitochondrial genome maintenance
GO:0000003 reproduction
alt_id: GO:0019952
alt_id: GO:0050876
GO:0000005 obsolete ribosomal chaperone activity
GO:0000006 high-affinity zinc uptake transmembrane transporter activity
GO:0000007 low-affinity zinc ion transmembrane transporter activity
GO:0000008 obsolete thioredoxin
alt_id: GO:0000013
GO:0000009 alpha-1,6-mannosyltransferase activity
它所说的alt_id
意思是它是以前GO:
代码的替代品。我想添加alt_id
前一个的定义GO:
,也就是说,我想要这样的输出:
GO:0000001 mitochondrion inheritance
GO:0000002 mitochondrial genome maintenance
GO:0000003 reproduction
alt_id: GO:0019952 reproduction
alt_id: GO:0050876 reproduction
GO:0000005 obsolete ribosomal chaperone activity
GO:0000006 high-affinity zinc uptake transmembrane transporter activity
GO:0000007 low-affinity zinc ion transmembrane transporter activity
GO:0000008 obsolete thioredoxin
alt_id: GO:0000013 obsolete thioredoxin
GO:0000009 alpha-1,6-mannosyltransferase activity
如何复制下面上一行的内容?我在基于 Windows 的环境中使用 Cygwin。
答案1
与awk
,不确定它是否会起作用Cygwin
$ awk '{ if(/^alt_id/){$0 = $0" "p} else{p = ""; for (i=2; i<=NF; i++) p = p" "$i} } 1' ip.txt
GO:0000001 mitochondrion inheritance
GO:0000002 mitochondrial genome maintenance
GO:0000003 reproduction
alt_id: GO:0019952 reproduction
alt_id: GO:0050876 reproduction
GO:0000005 obsolete ribosomal chaperone activity
GO:0000006 high-affinity zinc uptake transmembrane transporter activity
GO:0000007 low-affinity zinc ion transmembrane transporter activity
GO:0000008 obsolete thioredoxin
alt_id: GO:0000013 obsolete thioredoxin
GO:0000009 alpha-1,6-mannosyltransferase activity
alt_id
对于行首不匹配的每一行,使用变量 (p
) 保存从两行开始的所有列- 当行在行开头匹配时
alt_id
,将变量的内容附加到变量p
中包含的输入行$0
- 最后
1
是打印内容的快捷方式$0
答案2
该任务可以通过以下方式轻松完成sed
sed '
N #append next line (operate with `line1\nline2`);
/\nalt_id/s/\([^0-9]*\)\n.*/&\1/
#if next line starts with `alt_id` the append end of present line
P #print present line (all before `\n`)
D #remove all before `\n`, starts from begin with remain part (line2)
' file
另一种方法是使用保留空间
sed '
/^alt_id:/G #if line starts by `alt_id:` append hold-space
s/\n// #remove `\n`ewline symbol
t #if removing success pass further commands (go to end)
h #if no (for other lines) copy it to hold-space
s/\S*// #remove all non-space symbols from start till first space
x #exchange hold-space and pattern-space ==
#+put resedue into hold-space and return full line
' file