zoukankan      html  css  js  c++  java
  • [Bash]LeetCode195. 第十行 | Tenth Line

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10180443.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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.

    给定一个文本文件 file.txt,请只打印这个文件中的第十行。

    示例:

    假设 file.txt 有如下内容:

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

    你的脚本应当显示第十行:

    Line 10
    

    说明:
    1. 如果文件少于十行,你应当输出什么?
    2. 至少有三种不同的解法,请尝试尽可能多的方法来解题。


     4ms

    1 # Read from the file file.txt and output the tenth line to stdout.
    2 sed -n 10p file.txt

    4ms

    1 # Read from the file file.txt and output the tenth line to stdout.
    2 awk 'NR==10' file.txt

    8ms

    1 # Read from the file file.txt and output the tenth line to stdout.
    2 { cat file.txt; for (( i = 1; i <= 10; ++i)); do echo ; done } | head -n 10 | tail -n 1

    12ms

    1 # Read from the file file.txt and output the tenth line to stdout.
    2 
    3 sed 's/\n/
    4 /g' file.txt | awk 'NR==10{print}'
  • 相关阅读:
    left join问题
    SQL索引
    数据库查询优化
    define and inline
    程序的内存分配
    __closure
    this指针
    java笔记
    Visual Studio Code(VSCODE)语言设置
    Excel 2010如何打开多个独立窗口?
  • 原文地址:https://www.cnblogs.com/strengthen/p/10180443.html
Copyright © 2011-2022 走看看