【问题标题】:How to select characters from a line number using cut command如何使用剪切命令从行号中选择字符
【发布时间】:2015-01-08 17:43:10
【问题描述】:

我正在尝试查看是否有一种方法可以使用 cut 命令,但从指定的行号开始。

例如,我使用以下命令 cat 以下文件

cat -n client_custom.css

然后我得到了以下

  1 /* LEFT FRAME */
 2  /* Don't change this setting */
 3  /* Heading image setting */
 4  
 5  /* Use this line to repeat main_bg image */
 6  div#header{background:url(main_bg.jpg) 0 0 repeat-x;}
 7  /* Use this line if you have 1 big main_bg image and you don't want to repeat it 
 8  div#header{background:url(main_bg.jpg) 0 0 no-repeat;}
 9  */
10  
11  div#header-left{ background:url(left_bg.png) center 0px  no-repeat; width:100%; } 
12  

例如,我想在第 11 行削减 2,6。请问有没有办法做到这一点?

【问题讨论】:

  • 这是什么意思:“我想在第 11 行剪切 2,6”?显示,不要告诉

标签: bash shell unix terminal


【解决方案1】:

这是你所期望的吗?

$ grep '^11\b' file | cut -d ' ' -f2-6

输出:

 div#header-left{ background:url(left_bg.png) center 0px

【讨论】:

    【解决方案2】:

    我会看看 sed

    sed -n 11p
    

    Live On IdeOne

    这样你甚至不需要行编号(nl 也可用于编号行)

    【讨论】:

    • 感谢您的回复。那完成了工作。我是使用 unix 的新手。我认为 sed 仅用于查找和替换...我的问题是 '11p' 中的 'p' 代表什么?谢谢!
    • 我的问题是为什么我不能使用 grep 来使用它?
    • 基本上,因为 grep 是一个不同的程序。当然你也可以使用 grep (grep '^11 ' 开始)
    • 出于好奇,我尝试使用cat -n client_custom.css | grep '^11',但它返回空白.....
    • 这对我来说意味着你的行以空格开头,在这种情况下更有理由使用正确的工具:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多