Using sed, how do you print the first 'N' characters of a line?

前端 未结 6 1428
北荒
北荒 2020-12-22 20:28

Using sed what is an one liner to print the first n characters? I am doing the following:

grep -G \'defn -test.*\' OctaneFullTest.clj          


        
相关标签:
6条回答
  • 2020-12-22 20:51

    How about head ?

    echo alonglineoftext | head -c 9
    
    0 讨论(0)
  • 2020-12-22 20:56

    Don't use sed, use cut:

    grep .... | cut -c 1-N
    

    If you MUST use sed:

    grep ... | sed -e 's/^\(.\{12\}\).*/\1/'
    
    0 讨论(0)
  • 2020-12-22 21:04

    To print the N first characters you can remove the N+1 characters up to the end of line:

    $ sed 's/.//5g' <<< "defn-test"
    defn
    
    0 讨论(0)
  • 2020-12-22 21:08
    colrm x
    

    For example, if you need the first 100 characters:

    cat file |colrm 101 
    

    It's been around for years and is in most linux's and bsd's (freebsd for sure), usually by default. I can't remember ever having to type apt-get install colrm.

    0 讨论(0)
  • 2020-12-22 21:09

    Strictly with sed:

    grep ... | sed -e 's/^\(.\{N\}\).*$/\1/'
    
    0 讨论(0)
  • 2020-12-22 21:16

    don't have to use grep either

    an example:

    sed -n '/searchwords/{s/^\(.\{12\}\).*/\1/g;p}' file
    
    0 讨论(0)
提交回复
热议问题