Board logo

标题: [问题求助] [已解决]Linux命令截取字符串末尾的数字 [打印本页]

作者: newswan    时间: 2021-1-29 18:01     标题: [已解决]Linux命令截取字符串末尾的数字

本帖最后由 newswan 于 2021-1-29 23:27 编辑
  1. echo aa0| sed -r -e "s/.*([0-9]+)/\1/"
复制代码
但是,如果字符串末尾没数字,结果就出错
  1. echo aa| sed -r -e "s/.*([0-9]+)/\1/"
复制代码
请问,怎么解决
作者: netdzb    时间: 2021-1-29 19:47

回复 1# newswan


  加个$试试。
作者: newswan    时间: 2021-1-29 22:04

解决
  1. grep -Eo "[0-9]+$"
复制代码

作者: Batcher    时间: 2021-1-29 22:55

回复 1# newswan
  1. echo aa0|sed -r "/[0-9]$/!d;s/.*([0-9]+)/\1/"
  2. echo aa|sed -r "/[0-9]$/!d;s/.*([0-9]+)/\1/"
复制代码

作者: qixiaobin0715    时间: 2021-1-30 07:34

本帖最后由 qixiaobin0715 于 2021-1-30 07:38 编辑

回复 1# newswan
sed没用过,正则倒是接触过,说的不一定对:将.*[0-9]+改为[0-9]*试试
echo aa| sed -r -e "s/[0-9]*//"
作者: qixiaobin0715    时间: 2021-1-30 07:36

e是表示end吗
作者: newswan    时间: 2021-1-30 10:40

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -b, --binary
                 open files in binary mode (CR+LFs are not processed specially)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2