zoukankan      html  css  js  c++  java
  • bash leetcode

    拓展: grep

    193.  ref: https://blog.csdn.net/yanglingwell/article/details/82343407

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

    You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

    You may also assume each line in the text file must not contain leading or trailing white spaces.

    Example:

    Assume that file.txt has the following content:

    987-123-4567
    123 456 7890
    (123) 456-7890
    

    Your script should output the following valid phone numbers:

    987-123-4567
    (123) 456-7890

    grep -P '^(d{3}-|(d{3}) )d{3}-d{4}$' file.txt
    # 1. ^ 表示需要在行的开始处进行匹配. 例如, ^abc 可以匹配 abc123 但不匹配 123abc.
    # 2. $ 表示需要在行的末端进行匹配. 例如, abc$ 可以匹配 123abc 但不能匹配 abc123.
    # 3. d可以匹配一个数字.'00d'可以匹配'007',但无法匹配'00A'.
    # 4. {n}表示n个字符,用{n,m}表示n-m个字符. d{3}表示匹配3个数字,例如'010'.
    # 5. -P(grep): Interpret  the pattern as a Perl-compatible regular expression (PCRE).

    195. ref: https://blog.csdn.net/sole_cc/article/details/44977821

    Given a text file file.txt, print just the 10th line of the file.

    Example:

    Assume that file.txt has the following content:

    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    Line 6
    Line 7
    Line 8
    Line 9
    Line 10
    

    Your script should output the tenth line, which is:

    Line 10

    Note:
    1. If the file contains less than 10 lines, what should you output?
    2. There's at least three different solutions. Try to explore all possibilities.

    方法一:
    awk NR==10 file.txt

    //awk的默认动作就是打印$0,所以NR==10后面可以不用加{print $0}

    方法二:

    sed -n '10p' file.txt

    //如果不够10行,则什么也不打印

  • 相关阅读:
    k8s-学习笔记12-权限体系
    Linux上磁盘热插拔
    delphi hashmap
    my gcc project
    gcc dll 导出问题 GTK+Glade3 Gtk-WARNING **: Could not find signal handler 问题最终解析
    c/c++字符串定义及使用的对比
    gcc printf()打印char* str
    gcc选项-g与-rdynamic的异同
    GCC编译,库的编译使用及Makefile
    gcc test
  • 原文地址:https://www.cnblogs.com/ceceliahappycoding/p/11242580.html
Copyright © 2011-2022 走看看