当前位置:K88软件开发文章中心网站服务器框架Shell → 文章内容

Shell 字符串操作

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 14:39:35

4 5 6 7 8 9 10 11 12 13 14 1541 45 44 44 26 44 42 20 20 38 37 25 45 45 4516 17 18 19 20 21 22 23 24 25 26 27 28 29 3044 20 30 39 35 38 38 28 25 30 36 20 24 32 3331 32 33 34 35 36 37 38 39 40 41 42 43 44 4541 33 51 39 20 20 44 37 38 39 42 40 37 50 5046 47 48 49 50 51 52 53 54 55 5642 43 41 42 45 42 19 39 75 17 17$ cat data.txt | sort -k 2 -n1 2 3 4 5 6 7 8 9 10 11 12 13 14 1516 17 18 19 20 21 22 23 24 25 26 27 28 29 3044 20 30 39 35 38 38 28 25 30 36 20 24 32 3331 32 33 34 35 36 37 38 39 40 41 42 43 44 4541 33 51 39 20 20 44 37 38 39 42 40 37 50 5042 43 41 42 45 42 19 39 75 17 1741 45 44 44 26 44 42 20 20 38 37 25 45 45 4546 47 48 49 50 51 52 53 54 55 56子串进制转换如果字母和数字字符用来计数,那么就存在进制转换的问题。在《数值计算》一节,已经介绍了 bc 命令,这里再复习一下。$ echo "ibase=10;obase=16;10" | bcA说明: ibase 指定输入进制,obase 指出输出进制,这样通过调整 ibase 和 obase,你想怎么转就怎么转啦!子串编码转换什么是字符编码?这个就不用介绍了吧,看过那些乱七八糟显示的网页么?大多是因为浏览器显示时的”编码“和网页实际采用的”编码“不一致导致的。字符编码通常是指:把一序列”可打印“字符转换成二进制表示,而字符解码呢则是执行相反的过程,如果这两个过程不匹配,则出现了所谓的”乱码“。为了解决”乱码“问题呢?就需要进行编码转换。在 Linux 下,我们可以使用 iconv 这个工具来进行相关操作。这样的情况经常在不同的操作系统之间移动文件,不同的编辑器之间交换文件的时候遇到,目前在 Windows 下常用的汉字编码是 gb2312,而在 Linux 下则大多采用 utf8 。$ nihao_utf8=$(echo "你好")$ nihao_gb2312=$(echo $nihao_utf8 | iconv -f utf8 -t gb2312)字符串操作进阶实际上,在用 Bash 编程时,大部分时间都是在处理字符串,因此把这一节熟练掌握非常重要。正则表达式范例:处理 URL 地址URL 地址(URL(Uniform Resoure Locator:统一资源定位器)是WWW页的地址)几乎是我们日常生活的玩伴,我们已经到了无法离开它的地步啦,对它的操作很多,包括判断 URL 地址的有效性,截取地址的各个部分(服务器类型、服务器地址、端口、路径等)并对各个部分进行进一步的操作。下面我们来具体处理这个URL地址:ftp://anonymous:ftp@mirror.lzu.edu.cn/software/scim-1.4.7.tar.gz$ url="ftp://anonymous:ftp@mirror.lzu.edu.cn/software/scim-1.4.7.tar.gz"匹配URL地址,判断URL地址的有效性$ echo $url | grep "ftp://[a-z]*:[a-z]*@[a-z\./-]*"截取服务器类型$ echo ${url%%:*}ftp$ echo $url | cut -d":" -f 1ftp截取域名$ tmp=${url##*@} ; echo ${tmp%%/*}mirror.lzu.edu.cn截取路径$ tmp=${url##*@} ; echo ${tmp%/*}mirror.lzu.edu.cn/software截取文件名$ basename $urlscim-1.4.7.tar.gz$ echo ${url##*/}scim-1.4.7.tar.gz截取文件类型(扩展名)$ echo $url | sed -e 's/.*[0-9].\(.*\)/\1/g'tar.gz范例:匹配某个文件中的特定范围的行先准备一个测试文件READMEChapter 7 -- Exercises7.1 please execute the program: mainwithoutreturn, and print the return valueof it with the command "echo $?", and then compare the return of the printffunction, they are the same.7.2 it will depend on the exection mode, interactive or redirection to a file,if interactive, the "output" action will accur after the \n char with the linebuffer mode, else, it will be really "printed" after all of the strings havebeen stayed in the buffer.7.3 there is no another effective method in most OS. because argc and argv arenot global variables like environ.然后开始实验,打印出答案前指定行范围:第 7 行到第 9 行,刚好找出了第 2 题的答案$ sed -n 7,9p README7.2 it will depend on the exection mode, interactive or redirection to a file,if interactive, the "output" action will accur after the \n char with the linebuffer mode, else, it will be really "printed" after all of the strings have其实,因为这个文件内容格式很有特色,有更简单的办法$ awk '/7.2/,/^$/ {printf("%s\n", $0);}' README7.2 it will depend on the exection mode, interactive or redirection to a file,if interactive, the "output" action will accur after the \n char with the linebuffer mode, else, it will be really "printed" after all of the strings havebeen stayed in the buffer.有了上面的知识,就可以非常容易地进行这些工作啦:修改某个文件的文件名,比如调整它的编码,下载某个网页里头的所有 pdf 文档等。这些就作为练习自己做吧。处理格式化的文本平时做工作,大多数时候处理的都是一些“格式化”的文本,比如类似 /etc/passwd 这样的有固定行和列的文本,也有类似 tree 命令输出的那种具有树形结构的文本,当然还有其他具有特定结构的文本。关于树状结构的文本的处理,可以参考我早期写的另外一篇博客文章:源码分析:静态分析 C 程序函数调用关系图实际上,只要把握好特性结构的一些特点,并根据具体的应用场合,处理起来就不会困难。下面来介绍具体文本的操作,以 /etc/passwd 文件为例。关于这个文件的帮忙和用法,请通过 man 5 passwd 查看。下面对这个文件以及相关的文件进行一些有意义的操作。范例:选取指定列选取/etc/passwd文件中的用户名和组ID两列$ cat /etc/passwd | cut -d":" -f1,4选取/etc/group文件中的组名和组ID两列$ cat /etc/group | cut -d":" -f1,3范例:文件关联操作如果想找出所有用户所在的组,怎么办?$ join -o 1.1,2.1 -t":" -1 4 -2 3 /etc/passwd /etc/grouproot:rootbin:bindaemon:daemonadm:admlp:lppop:popnobody:nogroupfalcon:users说明: join 命令用来连接两个文件,有点类似于数据库的两个表的连接。 -t 指定分割符,-1 4 -2 3 指定按照第一个文件的第 4 列和第二个文件的第 3 列,即组 ID 进行连接,-o``1.1,2.1 表示仅仅输出

上一页  [1] [2] [3] [4] [5]  下一页


Shell 字符串操作