简体   繁体   中英

sed remove digits at end of the line

I need to find out how to delete up to 10 digits that are at the end of the line in my text file using sed.

For example if I have this:

ajsdlfkjasldf1234567890
asdlkjfalskdjf123456
adsf;lkjasldfkjas123

it should become:

ajsdlfkjasldf
asdlkjfalskdjf
adsf;lkjasldfkjas

can anyone help?

I have this, but its not working:

sed 's/[0-9]{10}$//g'

Have you tried this:

     sed 's/[0-9]+$//'

Your command would only match and delete exactly 10 digits at the end of line and only, if you enabled extended regular expressions (-E or -r, depending on your version of sed).

You should try

     sed -r 's/[0-9]{1,10}$//'

The following should work:

sed 's/[0-9]\{1,10\}$//' file

Regex syntax in sed requires backslashes before the brackets to use them for repetition, unless you use an extended regex option.

A quick look here suggests you should try this:

$ sed 's/[0-9]\{0,10\}$//g'

{ } should be escaped, unless you switch to extended regex syntax:

$ sed -r 's/[0-9]{0,10}$//g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM