Linux command (like cat) to read a specified quantity of characters

前端 未结 9 2068
梦谈多话
梦谈多话 2020-12-12 15:53

Is there a command like cat in linux which can return a specified quantity of characters from a file?

e.g., I have a text file like:

Hel         


        
9条回答
  •  不知归路
    2020-12-12 16:46

    Here's a simple script that wraps up using the dd approach mentioned here:

    extract_chars.sh

    #!/usr/bin/env bash
    
    function show_help()
    {
      IT="
    extracts characters X to Y from stdin or FILE
    usage: X Y {FILE}
    
    e.g. 
    
    2 10 /tmp/it     => extract chars 2-10 from /tmp/it
    EOF
      "
      echo "$IT"
      exit
    }
    
    if [ "$1" == "help" ]
    then
      show_help
    fi
    if [ -z "$1" ]
    then
      show_help
    fi
    
    FROM=$1
    TO=$2
    COUNT=`expr $TO - $FROM + 1`
    
    if [ -z "$3" ]
    then
      dd skip=$FROM count=$COUNT bs=1 2>/dev/null
    else
      dd skip=$FROM count=$COUNT bs=1 if=$3 2>/dev/null 
    fi
    

提交回复
热议问题