批处理之家's Archiver

newswan 发表于 2021-1-29 18:01

[已解决]Linux命令截取字符串末尾的数字

[i=s] 本帖最后由 newswan 于 2021-1-29 23:27 编辑 [/i]

[code]echo aa0| sed -r -e "s/.*([0-9]+)/\1/"[/code]但是,如果字符串末尾没数字,结果就出错[code]echo aa| sed -r -e "s/.*([0-9]+)/\1/"[/code]请问,怎么解决

netdzb 发表于 2021-1-29 19:47

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=235538&ptid=57698]1#[/url] [i]newswan[/i] [/b]


  加个$试试。

newswan 发表于 2021-1-29 22:04

解决[code]grep -Eo "[0-9]+$"[/code]

Batcher 发表于 2021-1-29 22:55

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=235538&ptid=57698]1#[/url] [i]newswan[/i] [/b][code]echo aa0|sed -r "/[0-9]$/!d;s/.*([0-9]+)/\1/"
echo aa|sed -r "/[0-9]$/!d;s/.*([0-9]+)/\1/"[/code]

qixiaobin0715 发表于 2021-1-30 07:34

[i=s] 本帖最后由 qixiaobin0715 于 2021-1-30 07:38 编辑 [/i]

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=235538&ptid=57698]1#[/url] [i]newswan[/i] [/b]
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: <[url]https://www.gnu.org/software/sed/[/url]>.
General help using GNU software: <[url]https://www.gnu.org/gethelp/[/url]>.

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.