在一行中组合两个sed命令

时间:2016-03-01 20:15:44

标签: unix awk sed

我正在寻找in place命令来更改以:.:.

结尾的所有文件行

chr01   1453173 .   C   T   655.85  PASS    .   GT:AD:DP:PGT:PID    0/1:25,29:54:.:.

chr01   1453173 .   C   T   655.85  PASS    .   GT:AD:DP    0/1:25,29:54

简而言之,我基本上会以:PGT:PID

结尾的任何行删除:.:.:.:.

4 个答案:

答案 0 :(得分:1)

您正在寻找类似的内容:

sed -i.bak "/^.*:\.:\.$/ {s/:PGT:PID//g; s/:\.:\.//g;}" file 
  • 它替换为文件,并以file.bak
  • 创建备份
  • /^.*:\.:\.$/将s命令限制为:.:. .需要引用的行,因为它们是正则表达式的特殊字符
  • s sommand用空字符串
  • 替换字符串

答案 1 :(得分:1)

用awk表示:

awk 'sub(/:\.:\.$/,""){sub(/:PGT:PID/,"")} 1' file
chr01   1453173 .   C   T   655.85  PASS    .   GT:AD:DP    0/1:25,29:54

使用gawk进行内部编辑,您可以添加-i inplace选项,而只需添加> tmp && mv tmp file即可添加<section id="pre-acquisition" class="section scrollspy" markdown="1"> content </section> <section id="accessioning" class="section scrollspy" markdown="1"> content <section id="accessioning-guidelines" class="section scrollspy" markdown="1"> more content </section> <section id="accessioning-templates" class="section scrollspy" markdown="1"> even more content </section> </section>

答案 2 :(得分:1)

使用GNU sed和Solaris sed:

sed '/:\.:\.$/{s///;s/PGT:PID//;}' file

如果您想使用GNU sed“就地”编辑文件,请使用选项-i

答案 3 :(得分:1)

As a one-liner:

sed -i -e '/:\.:\.$/!n' -e 's///' -e 's/:PGT:PID//g' "$file"

Expanded:

/:\.:\.$/!n     # leave lines untouched unless they end in ":.:."
s///            # replace the matched ":.:." with nothing
s/:PGT:PID//g   # replace ":PGT:PID" with nothing, everywhere

We use the -i flag to perform in-place edits. We pass each line as a separate -e expression for portability: some sed implementations allow several commands to be concatenated with ;, but that is not required by the POSIX standard.