Is there a cleaner way of getting the last N characters of every line?

后端 未结 4 1773
轮回少年
轮回少年 2020-12-30 08:27

To simplify the discussion, let N = 3.

My current approach to extracting the last three characters of every line in a file or stream is to use sed

4条回答
  •  灰色年华
    2020-12-30 08:59

    Pure bash solution:

    $ while read -r in; do echo "${in: -3}"; done
    hello
    llo
    $
    

    sed

    $ sed 's,.*\(.\{3\}\)$,\1,'
    hallo
    llo
    $
    

提交回复
热议问题