首页 > 学院 > 开发设计 > 正文

shell命令之文本处理

2019-11-08 20:06:48
字体:
来源:转载
供稿:网友

cut命令head命令tr命令排序Uniq命令paste

cut命令

获取每行的第三个字母

cut -c3 fileName

获取每行的第三至第五个字母

cut -c3-5 fileName

获取每行的第三和第五个字母

cut -c3,5 fileName

获取每行的前三个字母

cut -c-3 fileName

获取每行的第五个字母往后的字母

cut -c5- fileName

获取文本中的前三个字段

cut -f1-3 fileName //默认的文本分隔符为/t//输入1 New York, New York[10] 8,244,910 1 New York-Northern New Jersey-Long Island, NY-NJ-PA MSA 19,015,900 1 New York-Newark-Bridgeport, NY-NJ-CT-PA CSA 22,214,0832 Los Angeles, California 3,819,702 2 Los Angeles-Long Beach-Santa Ana, CA MSA 12,944,801 2 Los Angeles-Long Beach-Riverside, CA CSA 18,081,5693 Chicago, Illinois 2,707,120 3 Chicago-Joliet-Naperville, IL-IN-WI MSA 9,504,753 3 Chicago-Naperville-Michigan City, IL-IN-WI CSA 9,729,8254 Houston, Texas 2,145,146 4 Dallas-Fort Worth-Arlington, TX MSA 6,526,548 4 Washington-Baltimore-Northern Virginia, DC-MD-VA-WV CSA 8,718,0835 Philadelphia, Pennsylvania[11] 1,536,471 5 Houston-Sugar Land-Baytown, TX MSA 6,086,538 5 Boston-Worcester-Manchester, MA-RI-NH CSA 7,601,061//输出1 New York, New York[10] 8,244,9102 Los Angeles, California 3,819,7023 Chicago, Illinois 2,707,1204 Houston, Texas 2,145,1465 Philadelphia, Pennsylvania[11] 1,536,471

获取输入文本的第四个字段

cut -d' ' -f4 fileName**input**HelloWorldhow are you**output**HelloWorld//输入的三行最大字段都不超过4个,为什么只输出Hello和World呢?这题不太靠谱

输出某个字段或字段范围和输出某个字母或字符范围类似,不再重复

head命令

输出前20行

head -n20 fileName //不存在20行的话有多少行算多少行

输出第11到第20行

head -n20 fileName | tail -n10

输出前20个字符

head -c20 fileName //不存在20个字符的话有多少算多少

输出后20行

tail -n20 fileName //不存在20行的话有多少行算多少行

输出后20个字符

tail -c20 fileName //不存在20个字符的话有多少算多少

tr命令

将文本中的一些字母按先后顺序替换为另外一些字母

tr '()' '[]' fileName**Input**int i=(int)5.8(23 + 5)*2**Output**int i=[int]5.8[23 + 5]*2tr '[a-z]' '[A-Z]' fileName //将文本中的小写字母替换为大写字母

删除文本中存在的小写字母

tr -d '[a-z]' fileName**Input**HelloWorldhow are you**Output**HW

将多个连续出现的给定字符合并为一个

tr -s ' ' fileName //合并多个空格为一个**Input**He lloWor ldhow are you**Output**He lloWor ldhow are you

排序

对文本行进行排序

sort fileName**Input**Dr. Rajendra PRasad January 26, 1950 May 13, 1962Dr. S. Radhakrishnan May 13, 1962 May 13, 1967Dr. Zakir Hussain May 13, 1967 August 24, 1969Shri Varahagiri Venkata Giri August 24, 1969 August 24, 1974Shri Fakhruddin Ali Ahmed August 24, 1974 February 11, 1977Shri Neelam Sanjiva Reddy July 25, 1977 July 25, 198**Output**Dr. Rajendra Prasad January 26, 1950 May 13, 1962Dr. S. Radhakrishnan May 13, 1962 May 13, 1967Dr. Zakir Hussain May 13, 1967 August 24, 1969Shri Fakhruddin Ali Ahmed August 24, 1974 February 11, 1977Shri Neelam Sanjiva Reddy July 25, 1977 July 25, 198Shri Varahagiri Venkata Giri August 24, 1969 August 24, 1974sort -r fileName //降序排序

对数字进行排序 sort -rn fileName //对数字进行降序排序 -n对数字排序

对输入文本中第二个字段数字进行降序排序

sort -t$'/t' -nr fileName//-t$'/t' 以tab分割文本//-nr 数字降序排序**Input**Albany, N.Y. 22.2 46.6 71.1 49.3 38.60 136 64.4 57Albuquerque, N.M. 35.7 55.6 78.5 57.3 9.47 60 11.0 64Anchorage, Alaska 15.8 36.3 58.4 34.1 16.08 115 70.8 39 / 60Asheville, N.C. 35.8 54.1 73.0 55.2 47.07 126 15.3 39Atlanta, Ga. 42.7 61.6 80.0 62.8 50.20 115 2.1 69 / 65Atlantic City, N.J. 32.1 50.6 75.3 55.1 40.59 113 16.2 60 / 54Austin, Texas 50.2 68.3 84.2 70.6 33.65 85 0.9 62 / 58Baltimore, Md. 32.3 53.2 76.5 55.4 41.94 115 21.5 53Baton Rouge, La. 50.1 66.6 81.7 68.1 63.08 110 0.2 52 / 46Billings, Mont. 24.0 46.1 72.0 48.1 14.77 96 56.9 69Birmingham, Ala. 42.6 61.3 80.2 62.9 53.99 117 1.5 60Bismarck, N.D. 10.2 43.3 70.4 45.2 16.84 96 44.3 64Boise, Idaho 30.2 50.6 74.7 52.8 12.19 89 20.6 64Boston, Mass. 29.3 48.3 73.9 54.1 42.53 127 42.8 52 / 66Bridgeport, Conn. 29.9 48.9 74.0 54.7 44.15 119 26.2 55 / 49**Output**Austin, Texas 50.2 68.3 84.2 70.6 33.65 85 0.9 62 / 58Baton Rouge, La. 50.1 66.6 81.7 68.1 63.08 110 0.2 52 / 46Atlanta, Ga. 42.7 61.6 80.0 62.8 50.20 115 2.1 69 / 65Birmingham, Ala. 42.6 61.3 80.2 62.9 53.99 117 1.5 60Asheville, N.C. 35.8 54.1 73.0 55.2 47.07 126 15.3 39Albuquerque, N.M. 35.7 55.6 78.5 57.3 9.47 60 11.0 64Baltimore, Md. 32.3 53.2 76.5 55.4 41.94 115 21.5 53Atlantic City, N.J. 32.1 50.6 75.3 55.1 40.59 113 16.2 60 / 54Boise, Idaho 30.2 50.6 74.7 52.8 12.19 89 20.6 64Bridgeport, Conn. 29.9 48.9 74.0 54.7 44.15 119 26.2 55 / 49Boston, Mass. 29.3 48.3 73.9 54.1 42.53 127 42.8 52 / 66Billings, Mont. 24.0 46.1 72.0 48.1 14.77 96 56.9 69Albany, N.Y. 22.2 46.6 71.1 49.3 38.60 136 64.4 57Anchorage, Alaska 15.8 36.3 58.4 34.1 16.08 115 70.8 39 / 60Bismarck, N.D. 10.2 43.3 70.4 45.2 16.84 96 44.3 64sort -r fileName //降序排序

Uniq命令

uniq fieName//如果相邻行文本重复,只保留一行**Input**00000101000202**Output**00010002 uniq -u fileName fieName//获取行中与前后行都不同的行文本**Output**00uniq -d fileName fieName//获取文本中前后相同的行文本**Output**000102uniq -c | cut -c7- fieName//获取文本中的字段统计**Output**2 002 011 002 02uniq -ci | cut -c7- fieName//获取文本中的字段统计,不区分大小写如输入**Input**aAAaAAaa**output**4 aA

paste

paste -d':' fileName1 fileName2 //将两个文件按行合并,以':'分割paste -s fileName //将文本以'/t'为分隔符进行合并**Input**Albany, N.Y.Albuquerque, N.M.Anchorage, AlaskaAsheville, N.C.Atlanta, Ga.Atlantic City, N.J.Austin, TexasBaltimore, Md.Baton Rouge, La.Billings, Mont.Birmingham, Ala.Bismarck, N.D.Boise, IdahoBoston, Mass.Bridgeport, Conn.**Output**Albany, N.Y. Albuquerque, N.M. Anchorage, Alaska Asheville, N.C.Atlanta, Ga. Atlantic City, N.J. Austin, Texas Baltimore, Md. Baton Rouge, La. Billings, Mont. Birmingham, Ala. Bismarck, N.D. Boise, Idaho Boston, Mass. Bridgeport, Conn.paste - - - -d$'/t' fileName //将文本以'/t'为分隔符进行合并,每行三列**Input**Albany, N.Y.Albuquerque, N.M.Anchorage, AlaskaAsheville, N.C.Atlanta, Ga.Atlantic City, N.J.Austin, TexasBaltimore, Md.Baton Rouge, La.Billings, Mont.Birmingham, Ala.Bismarck, N.D.Boise, IdahoBoston, Mass.Bridgeport, Conn.**Output**Albany, N.Y. Albuquerque, N.M. Anchorage, AlaskaAsheville, N.C. Atlanta, Ga. Atlantic City, N.J.Austin, Texas Baltimore, Md. Baton Rouge, La.Billings, Mont. Birmingham, Ala. Bismarck, N.D.Boise, Idaho Boston, Mass. Bridgeport, Conn.paste -d';' -s fileName //将文本合并为一行,用';'分割**Output**Albany, N.Y.;Albuquerque, N.M.;Anchorage, Alaska;Asheville, N.C.;Atlanta, Ga.;Atlantic City, N.J.;Austin, Texas;Baltimore, Md.;Baton Rouge, La.;Billings, Mont.;Birmingham, Ala.;Bismarck, N.D.;Boise, Idaho;Boston, Mass.;Bridgeport, Conn.paste - - - -d';' //将文本合并为一行,用';'分割,每行三列**Output**Buffalo, N.Y.;Burlington, Vt.;Caribou, MaineCasper, Wyo.;Charleston, S.C.;Charleston, W.Va.

题目出处:https://www.hackerrank.com/domains/shell/textpro/page:1


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表