主頁(yè) > 知識(shí)庫(kù) > Linux shell 獲得字符串所在行數(shù)及位置的方法

Linux shell 獲得字符串所在行數(shù)及位置的方法

熱門(mén)標(biāo)簽:Linux服務(wù)器 呼叫中心市場(chǎng)需求 百度競(jìng)價(jià)排名 地方門(mén)戶網(wǎng)站 鐵路電話系統(tǒng) 網(wǎng)站排名優(yōu)化 AI電銷(xiāo) 服務(wù)外包

01 獲取字符串所在的行數(shù)

方式一:用grep -n

[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# cat test | grep -n exe
5:exe
[root@root]# cat test | grep -n exe | awk -F ":" '{print $1}'
5

方式二:用sed -n '/查詢的字符串/=' 文件

[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# 
[root@root]# sed -n '/exe/=' test
5

02 獲取字符串中字符所在的位置

方式一:用awk -F 和 wc -c 組合

[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}';
uellevcmp
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}' | wc -c
10

方式二:用awk 'BEGIN{print index("'${str}'","'${str1}'") }'

[root@root]# str='uellevcmpottcap';str1='ott';awk 'BEGIN{print index("'${str}'","'${str1}'") }'
10

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • linux shell判斷字符串為空的正確方法示例
  • Linux Shell 截取字符串的方法示例
  • Linux Shell 生成隨機(jī)數(shù)和隨機(jī)字符串的方法示例
  • linux shell字符串內(nèi)置的常用操作(獲取長(zhǎng)度、查找、替換)
  • linux shell 字符串操作(長(zhǎng)度,查找,替換)詳解
  • Linux shell腳本中字符串連接的方法

標(biāo)簽:黃山 湖南 崇左 湘潭 衡水 蘭州 銅川 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux shell 獲得字符串所在行數(shù)及位置的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266