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

后端 未结 4 1776
轮回少年
轮回少年 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 09:21

    It's very simple with grep -o '...$':

    cat /etc/passwd  | grep -o '...$'
    ash
    /sh
    /sh
    /sh
    ync
    /sh
    /sh
    /sh
    

    Or better yer:

    N=3; grep -o ".\{$N\}$" 

    That way you can adjust your N for whatever value you like.

提交回复
热议问题