linux 基本命令___0003 字串處理和yum安裝軟體的路徑

AdaWongCorner發表於2017-07-24

字串變數的處理

參考連結:SHELL字串處理技巧

計算字串的字元數量: ${#str}

str="xxx-Lane1_S2_L001_R1_trim.fastq"

echo $str
### xxx-Lane1_S2_L001_R1_trim.fastq

echo ${#str}
### 31

刪除VALUE字串中以分隔符“.”匹配的右邊字元,保留左邊字元: ${str%.*}${str%%.*}

str2=`ls xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz`

echo ${str2%.*} #單次匹配
### xxx-Lane1_S2_L001_R1_trim.fastq.tar

echo ${str2%%.*} #多次匹配
### xxx-Lane1_S2_L001_R1_trim

刪除VALUE字串中以分隔符“.”匹配的左邊字元,保留右邊字元: ${str#*.}${str##*.}

str2=`ls xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz`

echo ${str2#*.}
### fastq.tar.gz

echo ${str2##*.}
### .gz

用NEW子串替換str字串中匹配的OLD子串: ${str/OLD/NEW}${str//OLD/NEW}

str2=`ls xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz`

echo ${str2/tar.gz/bz}
### xxx-Lane1_S2_L001_R1_trim.fastq.bz

echo ${str3//_/|}
### xxx-Lane1|S2|L001|R1|trim.fastq.tar.gz

“*”表示萬用字元,用於匹配字串將被刪除的字串
非貪婪匹配:

  • “%”表示從右向左匹配;
  • “#”表示從左向右匹配;
  • “/”表示替換

貪婪匹配:

  • “%%”表示從右向左匹配;
  • “##”表示從左向右匹配;
  • “//”表示替換


從str字串的左邊開始中擷取子串: ${str:OFFSET}${str:OFFSET:LENGTH}

str2=`ls xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz`

echo ${str2:4:8}
### Lane1_S2

echo ${str2:4}
### Lane1_S2_L001_R1_trim.fastq.tar.gz

從str字串的右邊開始中擷取子串: ${str:0-OFFSET}${str:0-OFFSET:LENGTH}

str2=`ls xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz`

echo ${str2:4:8}
### Lane1_S2

echo ${str2:0-6}
### tar.gz


下例中,WORD可以為一個字串,也可以為一個變數。當為變數時,需要用“$”引用該變數。

var="xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz"
word="yyy"
str=""

# 1.  ${VALUE:-WORD}:當變數未定義或者值為空時,返回值為WORD的內容,否則返回變數的值。
echo ${str:-$word}
### yyy

echo ${var:-$word}
### xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz


# 2.  ${VALUE:=WORD}:當變數未定義或者值為空時,返回WORD的值的同時並將WORD賦值給VALUE,否則返回變數的值。
echo ${str:=$word}
### yyy
echo $str #此時str的值也發生了改變
### yyy

echo ${var:=$word}
### xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz


# 3.  ${VALUE:+WORD}:當變數已賦值時,其值才用WORD替換,否則不進行任何替換。
echo ${str:+$word}
###     

echo ${var:+$word}
yyy


# 4.  ${VALUE:?MESSAGE}:當變數已賦值時,正常替換。否則將訊息MESSAGE送到標準錯誤輸出(若此替換出現在SHELL程式中,那麼該程式將終止執行)。
echo ${str:?"warning"}
### bash: str: warning

echo ${var:?"warning"}
### xxx-Lane1_S2_L001_R1_trim.fastq.tar.gz



yum安裝軟體的路徑

以安裝BEDTools為例

#
sudo yum install BEDTools

rpm -q BEDTools
#BEDTools-2.15.0-1.el6.x86_64

rpm -qa |grep BEDTools
#BEDTools-2.15.0-1.el6.x86_64

rpm -ql BEDTools-2.15.0-1.el6.x86_64
# /usr/bin/annotateBed
# /usr/bin/bamToBed
# /usr/bin/bed12ToBed6
# /usr/bin/bedToBam
# /usr/bin/bedToIgv
# /usr/bin/bedpeToBam
# /usr/bin/bedtools
# /usr/bin/closestBed
# /usr/bin/clusterBed
# /usr/bin/complementBed
# /usr/bin/coverageBed
# /usr/bin/fastaFromBed
# /usr/bin/flankBed
# /usr/bin/genomeCoverageBed
# /usr/bin/getOverlap
# /usr/bin/groupBy
# /usr/bin/intersectBed
# /usr/bin/linksBed
# /usr/bin/maskFastaFromBed
# /usr/bin/mergeBed
# /usr/bin/multiBamCov
# /usr/bin/multiIntersectBed
# /usr/bin/nucBed
# /usr/bin/pairToBed
# /usr/bin/pairToPair
# /usr/bin/shuffleBed
# /usr/bin/slopBed
# /usr/bin/sortBed
# /usr/bin/subtractBed
# /usr/bin/tagBam
# /usr/bin/unionBedGraphs
# /usr/bin/windowBed
# /usr/bin/windowMaker
# /usr/share/BEDTools
# /usr/share/BEDTools/data
# /usr/share/BEDTools/data/knownGene.hg18.chr21.bed
# /usr/share/BEDTools/data/rmsk.hg18.chr21.bed
# /usr/share/BEDTools/genomes
# /usr/share/BEDTools/genomes/human.hg18.genome
# /usr/share/BEDTools/genomes/human.hg19.genome
# /usr/share/BEDTools/genomes/mouse.mm8.genome
# /usr/share/BEDTools/genomes/mouse.mm9.genome
# /usr/share/doc/BEDTools-2.15.0
# /usr/share/doc/BEDTools-2.15.0/LICENSE
# /usr/share/doc/BEDTools-2.15.0/README.rst
# /usr/share/doc/BEDTools-2.15.0/RELEASE_HISTORY

RPM

rpm 全名是 “RedHat Package Manager”,簡稱則為RPM。
RPM預設安裝路徑

  • /etc 設定檔案放置的目錄
  • /usr/bin 可執行檔案
  • /usr/lib 程式使用的動態函式庫
  • /usr/share/doc 基本的軟體使用手冊與幫助文件
  • /usr/share/man man page檔案

 

相關文章