shell命令之基本的数组操作
来源:互联网 发布:驱动精灵mac版下载 编辑:程序博客网 时间:2024/05/21 09:02
Arrays in bash
1.将多行文本合并为一行
i=0while read linedoarr[$i]=$line((i++))doneecho ${arr[@]} //echo ${arr[@]}输出所有的数组元素**Input**NamibiaNauruNepalNetherlandsNewZealandNicaraguaNigerNigeriaNorthKoreaNorway**Output**Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway
2.将包含‘a’字母的文本行删掉
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.将上述输入文本重复输出三次
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.输出某个元素
echo ${arr[3]}
5.统计输入文本有多少行
a.使用wc命令wc -lb.使用echo命令arr=($(cat))echo ${#arr[@]}c.使用for循环i=0while read linedoarr[$i]=$line((i++))doneecho "$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
0 0
- shell命令之基本的数组操作
- shell命令之基本的shell命令
- Shell编程之数组、字符串基本操作
- shell 中数组的基本操作
- HBase Shell的一些基本操作命令
- HBase Shell基本操作命令
- Hbase 基本shell操作命令
- hbase shell 基本操作命令
- shell命令进阶【Linux-shell基本操作】
- hadoop之hdfs的shell命令操作
- Shell 的基本命令
- shell的基本命令
- shell编程笔记——数组的基本操作
- shell的基本操作
- shell的基本操作
- 【shell】基本操作,和常见的linux内建命令。
- Javascript之数组的基本操作
- zeus之shell基本命令
- js完美运动框架最终版
- ionic2 数据更新,刷新页面
- 菜鸟java
- LintCode 6 合并排序数组
- 移植三星版本uboot_s5pv210
- shell命令之基本的数组操作
- Codeforces Round #367 (Div. 2) E. Working routine(十字链表)
- LaTeX/Tables
- C# .net中cookie值为中文时的乱码解决方法
- 01_考虑用静态工厂方法代替构造器
- GPIO八种工作模式总结
- 【React-Router】关于browserHistory出现did not match any routes的问题
- 结合Scikit-learn介绍几种常用的特征选择方法
- 环境字体对python字符串格式化显示效果的影响