shell命令之基本的数组操作

Arrays in bash

1.将多行文本合并为一行

i=0
while read line
do
arr[$i]=$line
((i++))
done
echo ${arr[@]}       //echo ${arr[@]}输出所有的数组元素

**Input**
Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway
**Output**
Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway

2.将包含‘a’字母的文本行删掉

i=0
while read line
do
arr[$i]=$line
((i++))
done
echo ${arr[@]/*[aA]*/}

**Input**
Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway
**Output**
Niger

**Output2**
echo ${arr[@]/*[aA]*/hello}  //使用hello替换掉所有包含a的文本行
hello hello hello hello hello hello Niger hello hello hello

3.将上述输入文本重复输出三次

X=$(paste -sd' ' fileName)
echo $X $X $X

X=$(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.输出某个元素

echo ${arr[3]}

5.统计输入文本有多少行

a.使用wc命令
wc -l

b.使用echo命令
arr=($(cat))
echo ${#arr[@]}

c.使用for循环
i=0
while read line
do
arr[$i]=$line
((i++))
done
echo "$i"

6.将每行第一个大写字母替换为.

a.使用sed命令仅替换第一个大写字母为.
sed 's/[A-Z]/./' | paste -sd ' '

b.使用数组的替换来实现
X=($(cat)) 
echo "${X[@]/[A-Z]/.}"

**Output**
.amibia .auru .epal .etherlands .ewZealand .icaragua .iger .igeria .orthKorea .orway

7.找出一组数据中落单的数

a.首先将' '替换为换行,然后对每行数据排序,获取只出现一次的数字
tr ' ' '\n' | sort | uniq -u

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值