国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

shell命令之基本的數(shù)組操作

2019-11-08 20:08:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Arrays in bash

1.將多行文本合并為一行

i=0while read linedoarr[$i]=$line((i++))doneecho ${arr[@]} //echo ${arr[@]}輸出所有的數(shù)組元素**Input**NamibiaNauruNepalNetherlandsNewZealandNicaraguaNigerNigeriaNorthKoreaNorway**Output**Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway

2.將包含‘a(chǎn)’字母的文本行刪掉

i=0while read linedoarr[$i]=$line((i++))doneecho ${arr[@]/*[aA]*/}**Input**NamibiaNauruNepalNetherlandsNewZealandNicaraguaNigerNigeriaNorthKoreaNorway**Output**Niger**Output2**echo ${arr[@]/*[aA]*/hello} //使用hello替換掉所有包含a的文本行hello hello hello hello hello hello Niger hello hello hello

3.將上述輸入文本重復(fù)輸出三次

X=$(paste -sd' ' fileName)echo $X $X $XX=$(cat fileName)echo $X $X $X**Output**Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway

4.輸出某個(gè)元素

echo ${arr[3]}

5.統(tǒng)計(jì)輸入文本有多少行

a.使用wc命令wc -lb.使用echo命令arr=($(cat))echo ${#arr[@]}c.使用for循環(huán)i=0while read linedoarr[$i]=$line((i++))doneecho "$i"

6.將每行第一個(gè)大寫(xiě)字母替換為.

a.使用sed命令僅替換第一個(gè)大寫(xiě)字母為.sed 's/[A-Z]/./' | paste -sd ' 'b.使用數(shù)組的替換來(lái)實(shí)現(xiàn)X=($(cat)) echo "${X[@]/[A-Z]/.}"**Output**.amibia .auru .epal .etherlands .ewZealand .icaragua .iger .igeria .orthKorea .orway

7.找出一組數(shù)據(jù)中落單的數(shù)

a.首先將' '替換為換行,然后對(duì)每行數(shù)據(jù)排序,獲取只出現(xiàn)一次的數(shù)字tr ' ' '/n' | sort | uniq -u

題目出處:https://www.hackerrank.com/domains/shell/arrays-in-bash/page:1


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 科技| 晋城| 蒙阴县| 庆安县| 夏津县| 翁牛特旗| 南开区| 舒兰市| 太保市| 巨野县| 巴南区| 武山县| 新邵县| 文安县| 河曲县| 综艺| 沙坪坝区| 宁安市| 桂林市| 龙海市| 沁水县| 韶关市| 济源市| 北安市| 柘荣县| 独山县| 建始县| 天全县| 吉木萨尔县| 灵丘县| 来安县| 水城县| 浦县| 利津县| 阳信县| 镇雄县| 望谟县| 逊克县| 馆陶县| 台安县| 新巴尔虎左旗|