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行,则什么也不打印

  • 相关阅读:
    ORACLE同义词
    eclipse如何检出项目的jar包
    2020-04-03
    十大经典排序算法
    2020-03-30
    world文档设置表格自定义序列
    2020面试记录
    java如何用一个循环实现两个有序数组合并成一个有序数组
    2020-1-8
    好物推荐之抽纸
  • 原文地址:https://www.cnblogs.com/ceceliahappycoding/p/11242580.html
Copyright © 2011-2022 走看看