我的文件如下:
HNRNPF-human_SRA:SRR4421749_1_ENCFF938GRX.fastq.gz
RBFOX2-human_SRA:SRR4421654_1_ENCFF187PBG.fastq.gz
U2AF2-human_SRA:SRR3469570_1_ENCFF158ZML.fastq.gz
HNRNPK-human_SRA:SRR3469488_2_ENCFF267TVR.fastq.gz
RBFOX2-human_SRA:SRR4421654_1_ENCFF588WPC.fastq.gz
U2AF2-human_SRA:SRR3469570_1_ENCFF550GXB.fastq.gz
我希望将它们重新标记为:
HNRNPF-human_SRA:SRR442174_ENCFF938GRX.fastq.gz
RBFOX2-human_SRA:SRR4421654_ENCFF187PBG.fastq.gz
U2AF2-human_SRA:SRR3469570_ENCFF158ZML.fastq.gz
HNRNPK-human_SRA:SRR3469488_ENCFF267TVR.fastq.gz
RBFOX2-human_SRA:SRR4421654_ENCFF588WPC.fastq.gz
U2AF2-human_SRA:SRR3469570_ENCFF550GXB.fastq.gz
即我删除两个'_'之间的数字,我一直尝试不同的命令,如gsub和split,但我只能在split命令中:
name=U2AF2-human_SRA:SRR3469570_1_ENCFF158ZML.fastq.gz
echo $name | awk '{split($0, arr, "[__]"); print arr[3]}'
答案 0 :(得分:2)
awk 解决方案。这样做:
$ awk -F_ -v OFS=_ '{print $1,$2,$4}' file
HNRNPF-human_SRA:SRR4421749_ENCFF938GRX.fastq.gz
RBFOX2-human_SRA:SRR4421654_ENCFF187PBG.fastq.gz
U2AF2-human_SRA:SRR3469570_ENCFF158ZML.fastq.gz
HNRNPK-human_SRA:SRR3469488_ENCFF267TVR.fastq.gz
RBFOX2-human_SRA:SRR4421654_ENCFF588WPC.fastq.gz
U2AF2-human_SRA:SRR3469570_ENCFF550GXB.fastq.gz
答案 1 :(得分:2)
使用awk
:
$ name="U2AF2-human_SRA:SRR3469570_1_ENCFF158ZML.fastq.gz"
$ awk 'sub(/_[0-9]+_/,"_")' <<<"$name"
U2AF2-human_SRA:SRR3469570_ENCFF158ZML.fastq.gz
保存在变量中:
$ myvar=$(awk 'sub(/_[0-9]+_/,"_")' <<<"$name")
$ echo "$myvar"
或Bash字符串替换
$ name="U2AF2-human_SRA:SRR3469570_1_ENCFF158ZML.fastq.gz"
$ echo "${name/_[0-9]*_/_}"
U2AF2-human_SRA:SRR3469570_ENCFF158ZML.fastq.gz
答案 2 :(得分:1)
要从字符串中删除所有_<digit>_
(将其替换为_
),只需使用简单的sed
代替
$ sed 's/_[0-9]_/_/g' file