sedの使い方まとめていきます | 今日から、俺は、遺伝子解析、始めます。

・1行目だけ削除

$sed -e '1d' file.txt

・1行目から5行目を削除

$sed -e '1,5d' file.txt

・Nを全て削除する

$sed 's/N//g' file.txt

・特定文字から特定文字までを抽出する

>less test.fasta

>test1

aaaaaaaaaaa

cccccccccc

tttttttttttttt

ggggggggggg

>test2

cccccccccc

aaaaaaaaaaa

>less test.fasta | sed -ne /">test1"/,/">"/p | sed '$d'

>test1

aaaaaaaaaaa

cccccccccc

tttttttttttttt

ggggggggggg

(test1から>列までを抽出して、最後の1行を消去)

 

・5行目を抽出

sed -n 5p file.txt

 

・シェルの中で行末の特定文字を変換(かつ変数を用いる)

#!/bin/sh

echo "######key in dir name with path#####"

read dir
echo "######key in alterfile name#######"
read file
cd ${dir}
for f
do
rename=$(less filename.txt | grep -w ${f} | head -1 | cut -f6)
less ./${file} | sed -e "s/${f}\$/${rename}/g" > ./middle
rm ${file}
mv ./middle ${file}

done

これでfilename.txtを参考に${file}ファイルのIDを変更できる

 

・特定文字(>)の後に文字を挿入

less test.fasta

>E1

atcc

>E2

gtcc

 

less test.txt | sed -e '/>/s/$/[E. coli]/g' > res.txt

less res.txt

>E1[E.coli]

atcc

>E2[E. coli]

gtcc

 

・ある文字をタブに置換(_をタブに置換)

sed s/_/'\t'/g test.txt

 

・特定文字がある行を削除

sed '/abc/d' input.file

sed '/>/d' input.fasta